[英]spring jersey POST serialized bean
有什么方法可以將POST
請求正文作為序列化的bean代替原始的json
string
嗎? 例如:
@POST
@Path(ResourceEndpoints.POST_USER_REGISTER)
@Produces(MediaType.APPLICATION_JSON)
public Response register(@RequestBody UserObject user) {
...
然后,我將不必在每個請求中編寫json序列化器樣板代碼。
目前,我必須這樣做:
@POST
@Path(ResourceEndpoints.POST_USER_REGISTER)
@Produces(MediaType.APPLICATION_JSON)
public Response register(@RequestBody String userObjectJson) {
Gson gson = new Gson();
UserObject userObject = gson.fromJson(userObjectJson, UserObject.class);
如果必須堅持使用Gson,則需要編寫MessageBodyReader
/ MessageBodyWriter
對以處理反序列化/序列化。 這就是將請求流轉換為我們的域對象和序列化為響應流的域對象的方式。
您可以在這里找到關於此主題的很好的解釋,並在此處將Gson集成到基於JAX-RS的應用程序中找到一個簡單的實現。 我將在底部發布實現(以防萬一)。
但是,您注冊所有其他Jersey組件(即資源類和提供程序)時, GsonMessageBodyHandler
以相同的方式注冊此GsonMessageBodyHandler
。 如果啟用了包掃描,則應從@Provider
批注中自動選擇@Provider
。 無論如何,都不需要@RequestBody
批注(我對Spring的工作不多,但是我想無論哪種方式都將其忽略)。
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import javax.ws.rs.Consumes;
import javax.ws.rs.Produces;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.ext.MessageBodyReader;
import javax.ws.rs.ext.MessageBodyWriter;
import javax.ws.rs.ext.Provider;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
@Provider
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public final class GsonMessageBodyHandler implements MessageBodyWriter<Object>,
MessageBodyReader<Object> {
private static final String UTF_8 = "UTF-8";
private Gson gson;
private Gson getGson() {
if (gson == null) {
final GsonBuilder gsonBuilder = new GsonBuilder();
gson = gsonBuilder.create();
}
return gson;
}
@Override
public boolean isReadable(Class<?> type, Type genericType,
java.lang.annotation.Annotation[] annotations, MediaType mediaType) {
return true;
}
@Override
public Object readFrom(Class<Object> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, String> httpHeaders, InputStream entityStream) throws Exception, ApplicationException {
InputStreamReader streamReader = new InputStreamReader(entityStream, UTF_8);
try {
Type jsonType;
if (type.equals(genericType)) {
jsonType = type;
} else {
jsonType = genericType;
}
return getGson().fromJson(streamReader, jsonType);
} finally {
streamReader.close();
}
}
@Override
public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
return true;
}
@Override
public long getSize(Object object, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
return -1;
}
@Override
public void writeTo(Object object, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException, WebApplicationException {
OutputStreamWriter writer = new OutputStreamWriter(entityStream, UTF_8);
try {
Type jsonType;
if (type.equals(genericType)) {
jsonType = type;
} else {
jsonType = genericType;
}
getGson().toJson(object, jsonType, writer);
} finally {
writer.close();
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.