有没有办法让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);
答案 0 :(得分:0)
如果你必须坚持Gson,你需要写一个MessageBodyReader
/ MessageBodyWriter
对来处理反序列化/序列化。这是将请求流转换为我们的域对象和序列化为响应流的域对象的方式。
您可以找到有关此主题here的详细说明,您可以找到一个简单的实现here at Integrating Gson into a JAX-RS based application。我将在底部发布实现(如果方面发生故障)。
但是,您注册所有其他Jersey组件,即资源类和提供程序,以相同的方式注册此GsonMessageBodyHandler
。如果启用了包扫描,则应从@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();
}
}
}