如何在Restful Web服务的Post方法中传递自定义对象

时间:2014-10-22 05:51:56

标签: java web-services rest

我要求在webservice端重新获取一些字段值,该字段值由客户端通过Post调用在自定义对象中传递,但是它会导致错误,如 -

org.jboss.resteasy.client.ClientResponseFailure: Unable to find a MessageBodyReader of content-type text/html;charset="utf-8" and type null
    at org.jboss.resteasy.client.core.BaseClientResponse.createResponseFailure(BaseClientResponse.java:522)
    at org.jboss.resteasy.client.core.BaseClientResponse.createResponseFailure(BaseClientResponse.java:513)
    at org.jboss.resteasy.client.core.BaseClientResponse.readFrom(BaseClientResponse.java:414)
    at org.jboss.resteasy.client.core.BaseClientResponse.getEntity(BaseClientResponse.java:376)
    at org.jboss.resteasy.client.core.BaseClientResponse.getEntity(BaseClientResponse.java:337)
    at com.rest.jso.mainclient.RestJsonClient.processPOSTRequest(RestJsonClient.java:49)
    at com.rest.jso.mainclient.RestJsonClient.main(RestJsonClient.java:33)

我的网络服务看起来像 -

@POST
    @Path("/update/{user}")
    @Produces("application/json")
    public Response updateRecord(@PathParam("user")String user) {

        User result = null;
        //User result = new User();
        try {
            result = new ObjectMapper().readValue(user, User.class);
        } catch (JsonParseException e) {
            e.printStackTrace();
        } catch (JsonMappingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        result.setName("Ram");
        return Response.status(200).entity(result).build();
    }

我的客户服务休息服务是 -

public static void processPOSTRequest() /*throws ResponseStatusNotOKException*/{
        User newUser = new User();
        newUser.setName("My");
        newUser.setId(22L);
        newUser.setAddress("nagar");

        ClientRequest clientRequest = new ClientRequest("http://localhost:8080/JsonRestExample/userService/update");
        clientRequest.accept(MediaType.TEXT_HTML_TYPE);
        ClientResponse<User> clientResponse = null;
        try {
            clientResponse = clientRequest.post(User.class);

        if(clientResponse != null && clientResponse.getResponseStatus().getStatusCode() == 200) {
            //User responseResult = clientResponse.getEntity();
            String json = new ObjectMapper().writeValueAsString(clientResponse.getEntity());
            System.out.println(json);
            //System.out.println("updated address-> "+responseResult.getAddress()+"id=> "+responseResult.getId()+"Name=> "+responseResult.getName());
        }else{
            throw new ResponseStatusNotOKException("Response status is not OK.");
        }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

我正在寻找根本原因,但仍然不知道。任何想法我该如何解决这个问题?

2 个答案:

答案 0 :(得分:0)

您可以尝试按xml格式发布数据,如下所示:

@POST
@Path("/update")
@Produces("application/xml")
public Response updateRecord(String requestXml) {

    User result = null;
    //User result = new User();
    try {
        result = fromXml(requestXml, User.class);      
    } catch (IOException e) {
        e.printStackTrace();
    }
    result.setName("Ram");
    return Response.status(200).entity(result).build();
}

发送http请求时,你需要将User类转换为xml String,然后发布它。

答案 1 :(得分:0)

问题是您没有定义您的请求的正文内容。 在您的客户端,您应该:

clientRequest.body("application/json", input);

其中input是以JSON编码的newUser。在此之后,您应该致电

 clientResponse = clientRequest.post(User.class);