support both Protobuf and JSON with Jersey Restful web service
6247 ワード
In one of my project、I developed the Reservice on Jersey and consume/produces Protobuf message.It all workd well until a new requirement came to add support of Javascript client. So,I added 4 more clases:
HextUtils
Json Format
Json Message Reader
Json Message Writer
along with two existing clases
ProtobufMessage Reader
ProtobufMessage Writer
now the web service can support both message formas.Just make sure to NOT have any media type explicity defined in the web service clases–Jersey will pick up the corect reader/writer based on the incomping contest-type and accept headers.
import comple.google.protobuf.Extension Registry;import comple.google.protobuf.Generate Message;
@Provider@Consumespublic class Json Messagagagageder Messagagagagagemens MessagagagagBodReader{public static String LS=System.getPropty(「ライン.separator」);prvate Extensininininstinininininininininininindedededededeststststreatededededededededededededededededededededededededededededededeststststststststststininininininininininininininininininininininininininininininininininininininininininininininining 2,MediaType mediaType){return mediaType.isComptible}
public T readFrom(Class type,TypennicType,Annotation[]annotations,MediaTypediaTypee,Multive aluedMap htthtHeaders,InputStream entityStream)throwIOException,WebApplectine Extration
newBuilddeder.invoke;StridentntitInputStream ToString;JsonFormat. merge(data、extentininingiststrybudentdededentinintttbuream);JsosonFormatomerge(data、eextentinininininininininingiststrystststststststststry,budedededededededededededededededededededededededededededededededentntntnttrtrtrtrtrtrtrtrtrtrtrtrtrtrtrtrtrtrtrtrtrtrtrtrtrtrtrattttttbudedededede
Strindate String convertInputStreamToString(InputStream io){StrigBuffer sb=new String Buffer();try{Bufferedereader=new Buffereader(new InputStreamReader(io));String.String.String.ininininininininininindededededededereader;Streader;Strialalalalalalalalalalalalalalalalalalalalalalininininininininininininininininininininininininininininininininininininininininin();} catch(IOException e){throw new RuntimeException(「Unbale to obtain an InputStream」、e)
’return sb.toString()}.
import comple.google.protobuf.Message;@Provider@Produces(「appication/json」)public class Json Message Writer implements Message BodyWriter{
@Overridepublic long getSize(T arg 0,Class arg 1,Typearg 2,Annotation[]arg 3,MediaType arg 4){return-1;]
@Overridepublic book isWriteable(Class arg 0、Type arg 1、Annotation[]arg 2、MediaType MediaType){return mediaType.isComptible(MediaType.APPLICATIONuSONuTYPE)
@Overridepublic void writeTo(T arg 0、Class arg 1、Typearg 2、Annotation[]arg 3、MediaTyyypearg 4、MultiivaldMap arg 5、OutputStream outputStream Stream)throwIOException、WebApple Expringggggggggggggggggggggggggggpppppppppppppppppupppppppupupppupupupupupupspspspspininininininininininininininininin
)
)
import comple.google.protobuf.GeneratMessage;import comple.google.protobuf.Message;
//***Message Body Reader.*/@Provider@Consumes(「appication/x-protobuf」)public class ProtoctobufMessage Reader implements Message BodyReader{public book isreadable(Class type,Type,Annotation[]annotations,Mediatype.Mediatyptage Mediatage Mediatble.cypteble Mediable Mediatble.cype.cype.cype.cype.cype.cype.cypture
public Message readFrom(Class type,Type generanitype,Annotation[]annotations,MediaTypediaType,Multive uedMap htHeaders,InputStream entityStream)throwIOException,WebApplectine Extraction
Generanted Message.Buider builder=(Generanted Message.Buider)newBuilder.invoke(type)
return builder.mergFrom(entityStream).build();catch(Exception){throw new WebApple Exception(e)}}
import java.io.IOException;import java.io.Output Stream;import java.lang.annotations.Annotation;import java.lang.reflect.Type;
import javax.ws.rs.Produces;import javax.ws.rs.WebApplication Exception;import javax.s.rs.com.MediaType;import javax.s.s.s.com.rs.Multive aluedMap;import javax.wster.exter.extjavaster.extjavage.extjavaster.extrer.extjacker.exypt.extjacker.exypt.exypt.exy
import comple.google.protobuf.Message;
//***A Message Body Writer*/@Provider@Produces(「appication/x-protobuf」)public class ProtoctobufMessage Writer implements Message BodyWriter{public book is Writeable(Class type、Type genercype、Annotation[]annotations、Mediape Mediage Mediage Mediage Mediatble Mediatble.cype.cype.cype.cypture Mediage Mediage Mediage Mediatble Mediage
public long getSize(Message m,Class type,Type generaicType,Annotation[]annotations,MediaType mediaType){return m.get Serialized Size()}
public void writeTo(Message m、Class type、Type generaicType、Annotation[]annotations、MediaType mediaType、Multive aluedMap httpHeaders、OutputStream entityStream)throwIOpteyStream Explation.
All crediits go to the people who have contributed and open-sourced the protobuf and json projjject.
HextUtils
Json Format
Json Message Reader
Json Message Writer
along with two existing clases
ProtobufMessage Reader
ProtobufMessage Writer
now the web service can support both message formas.Just make sure to NOT have any media type explicity defined in the web service clases–Jersey will pick up the corect reader/writer based on the incomping contest-type and accept headers.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import javax.ws.s.s.c.onsumes;import javax.ws.rs.WebApple Exception;import javax.ws.s.com re.MediaType;import javax.ws.rs.com re.MultivaaluedMap;import javax.ws.rs.ext.Message BodyReader;import javax.ws.s.ext.Provider;import comple.google.protobuf.Extension Registry;import comple.google.protobuf.Generate Message;
@Provider@Consumespublic class Json Messagagagageder Messagagagagagemens MessagagagagBodReader{public static String LS=System.getPropty(「ライン.separator」);prvate Extensininininstinininininininininininindedededededeststststreatededededededededededededededededededededededededededededededeststststststststststininininininininininininininininininininininininininininininininininininininininininininininining 2,MediaType mediaType){return mediaType.isComptible}
public T readFrom(Class type,TypennicType,Annotation[]annotations,MediaTypediaTypee,Multive aluedMap htthtHeaders,InputStream entityStream)throwIOException,WebApplectine Extration
newBuilddeder.invoke;StridentntitInputStream ToString;JsonFormat. merge(data、extentininingiststrybudentdededentinintttbuream);JsosonFormatomerge(data、eextentinininininininininingiststrystststststststststry,budedededededededededededededededededededededededededededededededentntntnttrtrtrtrtrtrtrtrtrtrtrtrtrtrtrtrtrtrtrtrtrtrtrtrtrtrtrattttttbudedededede
Strindate String convertInputStreamToString(InputStream io){StrigBuffer sb=new String Buffer();try{Bufferedereader=new Buffereader(new InputStreamReader(io));String.String.String.ininininininininininindededededededereader;Streader;Strialalalalalalalalalalalalalalalalalalalalalalininininininininininininininininininininininininininininininininininininininininin();} catch(IOException e){throw new RuntimeException(「Unbale to obtain an InputStream」、e)
’return sb.toString()}.
import java.io.IOException;
import java.io.OutputStream;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import javax.ws.rs.Produces;import javax.ws.rs.WebApplication Exception;import javax.s.rs.com.MediaType;import javax.s.s.s.com.rs.Multive aluedMap;import javax.wster.exter.extjavaster.extjavage.extjavaster.extrer.extjacker.exypt.extjacker.exypt.exypt.exyimport comple.google.protobuf.Message;@Provider@Produces(「appication/json」)public class Json Message Writer implements Message BodyWriter{
@Overridepublic long getSize(T arg 0,Class arg 1,Typearg 2,Annotation[]arg 3,MediaType arg 4){return-1;]
@Overridepublic book isWriteable(Class arg 0、Type arg 1、Annotation[]arg 2、MediaType MediaType){return mediaType.isComptible(MediaType.APPLICATIONuSONuTYPE)
@Overridepublic void writeTo(T arg 0、Class arg 1、Typearg 2、Annotation[]arg 3、MediaTyyypearg 4、MultiivaldMap arg 5、OutputStream outputStream Stream)throwIOException、WebApple Expringggggggggggggggggggggggggggpppppppppppppppppupppppppupupppupupupupupupspspspspininininininininininininininininin
)
)
import java.io.IOException;
import java.io.InputStream;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import javax.ws.s.rs.csosumes;import javax.ws.rs.WebApplication Exception;import javax.s.rs.com.MediaType;import javax.s.rs.com.Multive aluedMap;import javax.ws.javax.swers.exs.edge.extjavant.edge.edge.extjavant.edge.extjavant.extjavant.edge.edge.eximport comple.google.protobuf.GeneratMessage;import comple.google.protobuf.Message;
//***Message Body Reader.*/@Provider@Consumes(「appication/x-protobuf」)public class ProtoctobufMessage Reader implements Message BodyReader{public book isreadable(Class type,Type,Annotation[]annotations,Mediatype.Mediatyptage Mediatage Mediatble.cypteble Mediable Mediatble.cype.cype.cype.cype.cype.cype.cypture
public Message readFrom(Class type,Type generanitype,Annotation[]annotations,MediaTypediaType,Multive uedMap htHeaders,InputStream entityStream)throwIOException,WebApplectine Extraction
Generanted Message.Buider builder=(Generanted Message.Buider)newBuilder.invoke(type)
return builder.mergFrom(entityStream).build();catch(Exception){throw new WebApple Exception(e)}}
import java.io.IOException;import java.io.Output Stream;import java.lang.annotations.Annotation;import java.lang.reflect.Type;
import javax.ws.rs.Produces;import javax.ws.rs.WebApplication Exception;import javax.s.rs.com.MediaType;import javax.s.s.s.com.rs.Multive aluedMap;import javax.wster.exter.extjavaster.extjavage.extjavaster.extrer.extjacker.exypt.extjacker.exypt.exypt.exy
import comple.google.protobuf.Message;
//***A Message Body Writer*/@Provider@Produces(「appication/x-protobuf」)public class ProtoctobufMessage Writer implements Message BodyWriter{public book is Writeable(Class type、Type genercype、Annotation[]annotations、Mediape Mediage Mediage Mediage Mediatble Mediatble.cype.cype.cype.cypture Mediage Mediage Mediage Mediatble Mediage
public long getSize(Message m,Class type,Type generaicType,Annotation[]annotations,MediaType mediaType){return m.get Serialized Size()}
public void writeTo(Message m、Class type、Type generaicType、Annotation[]annotations、MediaType mediaType、Multive aluedMap httpHeaders、OutputStream entityStream)throwIOpteyStream Explation.
All crediits go to the people who have contributed and open-sourced the protobuf and json projjject.