Java 如何在restfullwebservice的Post方法中传递自定义对象
我需要在webservice端接收一些字段值,该值由客户机通过Post调用在自定义对象中传递,但它会导致如下错误-Java 如何在restfullwebservice的Post方法中传递自定义对象,java,web-services,rest,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.BaseClientResp
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)
我的Web服务看起来像-
@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();
}
}
我正在搜索根本原因,但仍然没有线索。知道如何解决此问题吗?您可以尝试按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请求时,您需要将用户类转换为xml字符串,然后发布它。问题在于您没有定义请求的正文内容。 在您的客户中,您应该:
clientRequest.body("application/json", input);
其中输入是用JSON编码的新用户。在这之后,只有你应该打电话
clientResponse = clientRequest.post(User.class);
您确定要发布任何内容吗?在代码中定义了一个新用户,但从未将其附加到post请求。