Jersey没有映射对JAVA Object的响应

时间:2016-10-12 09:49:23

标签: java web-services jersey pojo wildfly-10

我正在尝试使用Google时区API响应。我已经尝试了与此错误相关的Stack Overflow上的所有答案,但以下错误甚至没有改变:

ERROR [ClientResponse]:625 - A message body reader for Java class com.bystored.business.bo.timezone.TimeZoneResponse, and Java type class com.bystored.business.bo.timezone.TimeZoneResponse, and MIME media type application/json; charset=UTF-8 was not found
2016-10-12 14:39:36 ERROR [ClientResponse]:627 - The registered message body readers compatible with the MIME media type are:
application/json; charset=UTF-8 ->
com.sun.jersey.json.impl.provider.entity.JSONJAXBElementProvider$App
com.sun.jersey.json.impl.provider.entity.JSONArrayProvider$App
com.sun.jersey.json.impl.provider.entity.JSONObjectProvider$App
com.sun.jersey.json.impl.provider.entity.JSONRootElementProvider$App
com.sun.jersey.json.impl.provider.entity.JSONListElementProvider$App
*/* ->
com.sun.jersey.core.impl.provider.entity.FormProvider
com.sun.jersey.core.impl.provider.entity.MimeMultipartProvider
com.sun.jersey.core.impl.provider.entity.StringProvider
com.sun.jersey.core.impl.provider.entity.ByteArrayProvider
com.sun.jersey.core.impl.provider.entity.FileProvider
com.sun.jersey.core.impl.provider.entity.InputStreamProvider
com.sun.jersey.core.impl.provider.entity.DataSourceProvider
com.sun.jersey.core.impl.provider.entity.XMLJAXBElementProvider$General
com.sun.jersey.core.impl.provider.entity.ReaderProvider
com.sun.jersey.core.impl.provider.entity.DocumentProvider
com.sun.jersey.core.impl.provider.entity.SourceProvider$StreamSourceReader
com.sun.jersey.core.impl.provider.entity.SourceProvider$SAXSourceReader
com.sun.jersey.core.impl.provider.entity.SourceProvider$DOMSourceReader
com.sun.jersey.json.impl.provider.entity.JSONJAXBElementProvider$General
com.sun.jersey.json.impl.provider.entity.JSONArrayProvider$General
com.sun.jersey.json.impl.provider.entity.JSONObjectProvider$General
com.sun.jersey.core.impl.provider.entity.XMLRootElementProvider$General
com.sun.jersey.core.impl.provider.entity.XMLListElementProvider$General
com.sun.jersey.core.impl.provider.entity.XMLRootObjectProvider$General
com.sun.jersey.core.impl.provider.entity.EntityHolderReader
com.sun.jersey.json.impl.provider.entity.JSONRootElementProvider$General
com.sun.jersey.json.impl.provider.entity.JSONListElementProvider$General
com.sun.jersey.json.impl.provider.entity.JacksonProviderProxy

以下是我的JAVA课程:

public class TimeZoneResponse implements Serializable {
private long dstOffset;
private long rawOffset;
private String status;
private String timeZoneId;
private String timeZoneName;

@JsonProperty("dstOffset")
public long getDstOffset() {
    return dstOffset;
}

public void setDstOffset(long dstOffset) {
    this.dstOffset = dstOffset;
}

@JsonProperty("rawOffset")
public long getRawOffset() {
    return rawOffset;
}

public void setRawOffset(long rawOffset) {
    this.rawOffset = rawOffset;
}

@JsonProperty("status")
public String getStatus() {
    return status;
}

public void setStatus(String status) {
    this.status = status;
}

@JsonProperty("timeZoneId")
public String getTimeZoneId() {
    return timeZoneId;
}

public void setTimeZoneId(String timeZoneId) {
    this.timeZoneId = timeZoneId;
}

@JsonProperty("timeZoneName")
public String getTimeZoneName() {
    return timeZoneName;
}

public void setTimeZoneName(String timeZoneName) {
    this.timeZoneName = timeZoneName;
}

@Override
public String toString() {
    return "TimeZoneResponse{" +
            "dstOffset=" + dstOffset +
            ", rawOffset=" + rawOffset +
            ", status='" + status + '\'' +
            ", timeZoneId='" + timeZoneId + '\'' +
            ", timeZoneName='" + timeZoneName + '\'' +
            '}';
}
}

Jersey客户端GET方法如下:

public static <T extends Object> T get (String url, Map<String, String> queryParams, Map<String, String> headers, Class<? extends T> type) {
    Client client = Client.create();
    WebResource webResource = client.resource(url);

    if (queryParams != null) {
        for (String key : queryParams.keySet()) {
            webResource = webResource.queryParam(key, queryParams.get(key));
        }
    }

    WebResource.Builder builder = webResource.getRequestBuilder();
    if(headers != null && !headers.isEmpty()) {
        for(Map.Entry<String, String> entry : headers.entrySet()) {
            builder = webResource.header(entry.getKey(), entry.getValue());
        }
    }

    return builder.accept(MediaType.APPLICATION_JSON).get(type);

}

我在jersey-server中有jersey-bundle jersey-json pom的依赖关系。

1 个答案:

答案 0 :(得分:1)

就像您在服务器上配置POJOMappingFeature一样,您还需要在客户端上配置它

ClientConfig config = new DefaultClientConfig();
config.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, true);
Client client = Client.create(config);

自从我使用Jersey 1以来已经有一段时间了,所以如果上述情况不起作用,请使用config.getProperties(),但我认为它应该是getFeatures