プロトコル契約JDKのPBシーケンス

1つのPB契約

ことで合意、JDK PB JSON kryo容量のシーケンス

サーバ:

使用春ブーツ

	// HTTP本体容器サーバシリアライゼーション指定のPb 
		/ ** 
		 *いるProtobufシリアライゼーション
		 * / 
		@Bean 
		ProtobufHttpMessageConverter protobufHttpMessageConverter(){ 
			戻り新しい新しいProtobufHttpMessageConverter(); 
		} 

		/ ** 
		 *いるProtobufデシリアライズ
		 * / 
		@Bean 
		RestTemplate RestTemplate (protobufHttpMessageConverter protobufHttpMessageConverter)は{ 
			新しい新しいRestTemplate(Collections.singletonList(protobufHttpMessageConverter))を返します; 
		}

 

2つのBeanはPBのシーケンスを使用して、springbootに語りました

@Controller 
@RequestMapping( "/ PB")
パブリッククラスPbController { 

    @RequestMapping(値= "/テスト"は、= "アプリケーション/ X-いるProtobuf"を生成)
    @ResponseBody 
    公共MyBaseProto.BaseProto getPersonProto(@RequestBody MyBaseProto.BaseProtoリクエスト){ 

        MyBaseProto.BaseProto.Builderビルダー= MyBaseProto.BaseProto.newBuilder()。
        builder.setCode(request.getCode()+ 1)。
        builder.setMsg(request.getMsg()+ "バック"); 
        System.out.println(request.getCode()+ request.getMsg())。
        リターンbuilder.build(); 
    } 
}

 

クライアント:

オブジェクト=新しい新しいのjava.net.URL( "http://127.0.0.1:8080/pb/test")URL; 
            HttpURLConnectionの= CON(HttpURLConnectionの)object.openConnection(); 
            con.setDoOutput(真の); 
            con.setDoInput(真の); 

            //文しなければならず、そうでない場合は、アプリケーション/ Xデフォルト- WWW-form-urlencodedでの;のcharset = UTF-8 
            // * / *作業、春ブートサーバーが認識しないしない
            con.setRequestProperty( "Content-Typeのを"、 " / X-いるProtobufファイルアプリケーション"); 

            //文を省略してもよい
// con.setRequestProperty(" ""受け入れファイルアプリケーション/ X-いるProtobuf "); 

            //文を省略してもよい
// con.setRequestMethod(" POST「)。

            // JSONプロトコル、ここ行う
            / ** 
            JSONObject JSONObject新しい新しいData =();
            JSONObject電話番号は=新しいJSONObject(); 
            バイト[] PB = builder.build()。
            tel.put( "nationcode"、nationCode)。
            文字列の電話=にphoneNumber; 
            tel.put( "電話"、携帯電話); 
            data.put( "タイプ"、 "0")。
            data.put( "MSG"、コンテンツ)。
            文字列SIG = stringMD5(APPKEY.concat(電話))。
            data.put( "SIG"、SIG)。
            data.put( "TEL"、電話番号); 
             * / 

            // PB协议、自定义HTTP协议
            MyBaseProto.BaseProto.Builderビルダー= MyBaseProto.BaseProto.newBuilder(); 
            builder.setCode(1)。
            builder.setMsg( "試験")。
            OutputStream WR = con.getOutputStream()。
            wr.write(PB)。

            //显示POST请求返回的内容
            StringBuilderのSB =新しいStringBuilderの(); 
            INT HttpResult = con.getResponseCode()。
            IF(HttpResult == HttpURLConnection.HTTP_OK){ 
                InputStream InputStreamは= con.getInputStream()。
                ByteArrayOutputStream結果=新しいByteArrayOutputStream(); 
                バイト[] =新しいバイト[1024]バッファと 
                int型の長さ。
                (!(長さ= inputStream.read(バッファ))= -1){一方
                    result.write(バッファ、0、長さ)。
                }
                MyBaseProto.BaseProto baseProto = MyBaseProto.BaseProto.parseFrom(result.toByteArray())。
                System.out.println(baseProto.getCode()+ baseProto.getMsg())。

            } {他
                のSystem.out.println( "HTTPエラー")。
            }

 

参考ます。https://blog.csdn.net/u013219624/article/details/83152806

 

2 JDKシリアライズ

サーバー:使用桟橋

輸入java.io. *; 

インポートのjavax.servlet。*; 
輸入javax.servlet.http.HttpServlet。
インポートのjavax.servlet.http.HttpServletRequest; 
インポートのjavax.servlet.http.HttpServletResponse; 

輸入org.eclipse.jetty.server.Server; 
輸入org.eclipse.jetty.servlet.ServletContextHandler; 
輸入org.eclipse.jetty.servlet.ServletHolder。
輸入serial.MyBaseBean; 

パブリッククラスEmbeddingJettyWithServlet { 

    公共の静的な無効メイン(文字列[]引数)は例外{スロー

        Serverサーバー=新しいサーバ(8080)。

        ServletContextHandlerコンテキストが新しいServletContextHandler(ServletContextHandler.SESSIONS)を=。
        context.setContextPath( "/ JDK"); 
        server.setHandler(コンテキスト)。

        context.addServlet(新しいServletHolder(新しいHelloServlet())、 "/テスト")。
        server.start(); 

    } 

    パブリック静的クラスHelloServletはHttpServletのが延びる{ 

        @Overrideは、
        ボイドのdoGet(HttpServletRequestのREQ、HttpServletResponseのRESP)ServletExceptionがは、IOException {スロー保護
           dohandle(REQ、RESP)を、
        } 

        @Overrideが
        保護ボイドdoPostメソッドは、(HttpServletRequestのREQ、HttpServletResponseのRESP)ServletExceptionがは、IOException {スロー
            dohandle(REQ、RESP)を、
        } 

        プライベートボイドdohandle(HttpServletRequestのREQ、HttpServletResponseのRESP)はServletExceptionが、IOExceptionが{スロー

            MyBaseBean myBaseBean = NULL; 
            試す{ 
                InputStream InputStreamは= req.getInputStream()。
                ObjectInputStreamのObjectInputStreamの=新しいObjectInputStreamの(InputStreamの); 

                試す{ 
                    myBaseBean =(MyBaseBean)objectInputStream.readObject(); 
                }キャッチ(ClassNotFoundExceptionが電子){ 
                    e.printStackTrace(); 
                } 

                のSystem.out.println( "get--" + myBaseBean.getCode()+ myBaseBean.getMsg())。

            }キャッチ(例外e){ 
                e.printStackTrace(); 
            } 
 
            IF(myBaseBean == NULL)
                myBaseBean =新しいMyBaseBean()。

            myBaseBean.setCode(myBaseBean.getCode()+ 1)。
            myBaseBean.setMsg(myBaseBean.getMsg()+ "バック"); 

            resp.setContentType( "text / htmlの"); 
            resp.setStatus(HttpServletResponse.SC_OK)。


            ByteArrayOutputStream byteArrayOutputStream =新しいByteArrayOutputStream(); 
            ObjectOutputStreamのObjectOutputStreamの=新しいObjectOutputStreamの(byteArrayOutputStream)。
            objectOutputStream.writeObject(myBaseBean)。

            OutputStreamのOutputStream = resp.getOutputStream()。
            outputStream.write(byteArrayOutputStream.toByteArray())。
            outputStream.flush(); 
        }
    }

 

アクセスするには、ブラウザます。http:// localhost:8080 / JDK /テスト

SR serial.MyBaseBean襹S臥H <?I CODEL msgt Ljava /ラング/文字列;???????XP T testback

 

クライアント:

URLオブジェクト=新しいURL( "http://127.0.0.1:8080/jdk/test"); 
            HttpURLConnectionのCON =(HttpURLConnectionの)object.openConnection(); 
            con.setDoOutput(真の); 
            con.setDoInput(真の); 


            // JDK协议、自定义HTTP协议
            MyBaseBean myBaseBean =新しいMyBaseBean(); 
            ByteArrayOutputStream byteArrayOutputStream =新しいByteArrayOutputStream(); 
            ObjectOutputStreamのObjectOutputStreamの=新しいObjectOutputStreamの(byteArrayOutputStream)。
            objectOutputStream.writeObject(myBaseBean)。
            objectOutputStream.flush(); 
            バイト[] PB = byteArrayOutputStream.toByteArray()。
            OutputStream WR = con.getOutputStream()。
            wr.write(PB)。
            wr.flush(); 

            //显示POST请求返回的内容
            StringBuilderのSB =新しいStringBuilderの(); 
            INT HttpResult = con.getResponseCode()。
            IF(HttpResult == HttpURLConnection.HTTP_OK){ 
                InputStream InputStreamは= con.getInputStream()。
                ObjectInputStreamのObjectInputStreamの=新しいObjectInputStreamの(InputStreamの); 
                MyBaseBean myBaseBean1 =(MyBaseBean)objectInputStream.readObject(); 
                System.out.println(myBaseBean1.getCode()+ myBaseBean1.getMsg())。

            } {他
                のSystem.out.println( "HTTPエラー")。
            }

 

参考ます。http://www.mamicode.com/info-detail-2224013.html 

おすすめ

転載: www.cnblogs.com/silyvin/p/11892748.html