泽西休息服务不消耗JSON



我有一个休息服务和客户端。我正在尝试调用此服务以直接消耗JSON并将其转换为所需的对象。但这不起作用。我收到以下错误:Java类Com.a.b.c.d和Java类型类COM.A.B.C.D和MIME Media Type Application/json的消息主体阅读器。

服务:

@Path("/getListPrice")
public class ListPriceService {
      @POST
      @Consumes(MediaType.APPLICATION_JSON)
      @Produces(MediaType.APPLICATION_JSON)
      @Type(PricingObject.class)
      public Response search(PricingObject pricingObject, @Context final HttpHeaders headers) {
              .........
              return Response.ok().entity(pricingObject).build();
      }
}

客户端:

WebResource webResource = client.resource(url);               
ClientResponse response = webResource.accept(MediaType.APPLICATION_JSON)
                          .type(MediaType.APPLICATION_JSON_TYPE)
                          .post(ClientResponse.class, pricingObjectRequest);
if (response.getStatus() != 200) {
   throw new RuntimeException("Failed : HTTP error code : " + response.getStatus());
}

有人可以告诉我出了什么问题吗?

java类Com.a.b.c.d和Java类型类COM.A.B.C.D和MIME Media Type应用程序/JSON的消息主体阅读器

>

您没有说您是否获得了服务器端异常或客户端异常。如果前者,您要么没有JSON的提供商,要么没有配置。

这是提供商

<dependency>
    <groupId>com.sun.jersey</groupId>
    <artifactId>jersey-json</artifactId>
    <version>${jersey1-version}</version>
</dependency>

这是Web.xml配置

<init-param>
    <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
    <param-value>true</param-value>
</init-param>

如果是客户端的例外,我会假设您具有上述依赖性。然后只需将其与客户端配置

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

您需要配置泽西岛以使用hander json->对象映射。使用泽西岛1您需要在依赖项中添加JSON提供商,例如

<dependency>
    <groupId>com.sun.jersey</groupId>
    <artifactId>jersey-json</artifactId>
    <version>1.17.1</version>
</dependency>

您可以在下面修改您的资源类别

  /**<code>
     * {
     * "id"=1,
     * "name="priceitem1"
     * }
     * </code>
    **/
    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public Response search(PricingObject pricingObject) {
        JSONArray jsarray=new JSONArray();
        jsarray.put(pricingObject);
        return Response.ok().entity(jsarray.toString()).build();
    }

数据whci在 tags are the json data that will come from the client.

you can configure your bean class as below

public class PricingObject {
@XmlElement(name="id")
private int id;
@XmlElement(name="name")
private String name;
public int getId() {
    return id;
}
public void setId(int id) {
    this.id = id;
}
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
 之间

}

@xmlelement将使用您的bean类变量绑定JSON键。

最新更新