[英]Jersey doesn't work with given provider
我正在與Jersey合作,嘗試使用Gson提供程序而不是Jackson。 我使用了Moritz Post的Blogpost。
我定義的REST服務:@Path(“ user”)
public class UserResource extends AbstractResource {
@POST
@Path("auth")
@Consumes(MediaType.APPLICATION_JSON)
public String postAuth(Login obj) {
// Session session = HibernateHelper.getSessionFactory().openSession();
//Login obj = new Gson().fromJson(json, Login.class);
return "Response: " + obj;
}
}
它返回:響應:null null
當我明確使用Gson提供程序時:
@Path("user")
public class UserResource extends AbstractResource {
@POST
@Path("auth")
@Consumes(MediaType.APPLICATION_JSON)
public String postAuth(String json) {
Login obj = new Gson().fromJson(json, Login.class);
return "Response: " + obj;
}
}
它返回:響應:用戶名密碼
我在Grizzly明確注冊了提供商:
rc.register(GsonProvider.class);
提供程序的以下代碼:
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
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 GsonProvider implements MessageBodyWriter<Object>,
MessageBodyReader<Object> {
private static final String UTF_8 = "UTF-8";
private Gson gson;
private Gson getGson() {
System.out.println("ja");
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 IOException {
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();
}
}
}
我在github上添加了此案例的示例(使用Gson代替Jackson)。
您可以在此處找到GsonUtil的實現+整個示例:
https://github.com/DominikAngerer/java-GsonJerseyProvider
希望對您有所幫助-您還將看到一個用於配置整個提供程序的示例web.xml。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.