Rest Glassfish 4中Jersey 2.5的JsonProvider未映射属性
我使用jersey作为rest客户端,但当应用程序实际部署为Glassfish 4上的war时,我遇到了问题。当我从测试运行它时,它工作正常,但在部署的应用程序映射到POJO时,所有属性都返回null 我尝试了MoxyJsonProvider,JacksonJsonProvider也定制了GsonProvider,但结果是一样的 客户端构造函数: 获取方法之一: 目前使用的Gson提供商: 已部署应用程序的输出:Rest Glassfish 4中Jersey 2.5的JsonProvider未映射属性,rest,glassfish,jersey,gson,pojo,Rest,Glassfish,Jersey,Gson,Pojo,我使用jersey作为rest客户端,但当应用程序实际部署为Glassfish 4上的war时,我遇到了问题。当我从测试运行它时,它工作正常,但在部署的应用程序映射到POJO时,所有属性都返回null 我尝试了MoxyJsonProvider,JacksonJsonProvider也定制了GsonProvider,但结果是一样的 客户端构造函数: 获取方法之一: 目前使用的Gson提供商: 已部署应用程序的输出: [Container{id=null, command=null, imag
[Container{id=null, command=null, image=null, created=0, status=null, ports=null, size=0, sizeRootFs=0, names=null}, Container{id=null, command=null, image=null, created=0, status=null, ports=null, size=0, sizeRootFs=0, names=null}]
有人能告诉我为什么它在玻璃鱼身上不起作用吗?或者我做错了什么
谢谢。正如我要检查的第一件事,您是否尝试打印streamReader的内容?当我以单元测试的方式运行streamReader时,streamReader包含预期的JSON,但在glassfish中创建singleton时,它不会写入System.out流的内容,因此我认为glassfish中使用的是不同的提供程序,可能是默认的MOXy提供程序。
@Override
public List<Container> getContainers(boolean all, boolean latest, int limit, boolean showSize, String since, String before) throws DockerException {
Response response = restClient
.target(restURL)
.path(CONTAINERS_LIST)
.queryParam("all", all)
.queryParam("limit", limit)
.queryParam("since", since)
.queryParam("before", before)
.queryParam("size", showSize)
.request(MediaType.APPLICATION_JSON)
.get();
switch (response.getStatus()) {
case 200:
LOGGER.info("Container list succesfully retrieved");
break;
case 400:
LOGGER.error("Bad request parameter");
throw new DockerException("Bad request parameter");
case 500:
LOGGER.error("Docker Server Error");
throw new DockerException("Docker Server Error");
default:
throw new DockerException("Unknown Error");
}
Type type = new TypeToken<Collection<Container>>() {
}.getType();
List<Container> result = response.readEntity(new GenericType<List<Container>>() { });
LOGGER.debug(String.format("Response: %s", result));
return result;
}
@SerializedName("Id")
private String id;
@SerializedName("Command")
private String command;
@SerializedName("Image")
private String image;
@SerializedName("Created")
private long created;
@SerializedName("Status")
private String status;
@SerializedName("Ports")
private Port[] ports; //Example value "49164->6900, 49165->7100"
@SerializedName("SizeRw")
private int size;
@SerializedName("SizeRootFs")
private int sizeRootFs;
@SerializedName("Names")
private String[] names;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getCommand() {
return command;
}
public void setCommand(String command) {
this.command = command;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
public long getCreated() {
return created;
}
public void setCreated(long created) {
this.created = created;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public Port[] getPorts() {
return ports;
}
public void setPorts(Port[] ports) {
this.ports = ports;
}
public int getSize() {
return size;
}
public void setSize(int size) {
this.size = size;
}
public int getSizeRootFs() {
return sizeRootFs;
}
public void setSizeRootFs(int sizeRootFs) {
this.sizeRootFs = sizeRootFs;
}
public String[] getNames() {
return names;
}
public void setNames(String[] names) {
this.names = names;
}
@Provider
@Consumes({MediaType.APPLICATION_JSON, "text/json"})
@Produces({MediaType.APPLICATION_JSON, "text/json"})
public class GsonProvider implements MessageBodyWriter<Object>,
MessageBodyReader<Object> {
private static final String UTF_8 = "UTF-8";
private Gson gson;
public GsonProvider() {
}
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 IOException, WebApplicationException {
try (InputStreamReader streamReader = new InputStreamReader(entityStream, UTF_8)) {
Type jsonType;
if (type.equals(genericType)) {
jsonType = type;
} else {
jsonType = genericType;
}
return getGson().fromJson(streamReader, jsonType);
}
}
@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 {
try (OutputStreamWriter writer = new OutputStreamWriter(entityStream, UTF_8)) {
Type jsonType;
if (type.equals(genericType)) {
jsonType = type;
} else {
jsonType = genericType;
}
getGson().toJson(object, jsonType, writer);
}
}
}
Container{id=a9e3a67979d392a0e48d534db5184ce717b0629c4255d4dbc1373f5a9140df51, command=/bin/sh -c /usr/sbin/sshd -D, image=frantiseks/apac:latest, created=1388002251, status=Up About an hour, ports=[Lcz.utb.fai.apac.entity.Port;@69ac536b, size=0, sizeRootFs=0, names=[Ljava.lang.String;@3098cc00}
[Container{id=null, command=null, image=null, created=0, status=null, ports=null, size=0, sizeRootFs=0, names=null}, Container{id=null, command=null, image=null, created=0, status=null, ports=null, size=0, sizeRootFs=0, names=null}]