简体   繁体   中英

How to pass custom object in Post method of Restfull webservice

I have a requirement to recive some field value at webservice end which is passed by client in a custom object through Post call but its causes error like -

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)

My webservice looks like -

@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();
    }

My client to comsume the Rest service is -

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();
        }
    }

I'm searching for the root cause but still clue less .Any Idea how can I resolve this?

you can try post data by xml format, like this:

@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();
}

while send http request,u need to convert User class to xml String,and then POST it.

The problem is that you don't define your request's body content. In your client you should do :

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

Where input is your newUser encoded in JSON. After this only you should call

 clientResponse = clientRequest.post(User.class);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM