简体   繁体   中英

Jersey client request to web-service

I`m trying to request to web-service by jersey client:

WebResource service = client.resource(UriBuilder.fromUri("http://localhost:8080/jersey-example-new/").build());
System.out.println(service.path("rs/").path("account/details/1").accept(MediaType.APPLICATION_JSON).get(String.class));

but I get:

GET http://localhost:8080/jersey-example-new/rs/account/details/1 returned a response status of 406 Not Acceptable

Please, note that url path http://localhost:8080/jersey-example-new/rs/account/details/1 works in browser. What is wrong with java client request?

the endpoint code:

@Path("account")
public class AccountDetailsService {

    @GET
    @Path("/details/{param}")
    @Produces(MediaType.TEXT_PLAIN)
    public Response getAccountDetails(@PathParam("param") String accountName) {
        String output = "Account Name : " + accountName;
        return Response.status(200).entity(output).build();
    }
}

You should change

System.out.println(service.path("rs/").path("account/details/1").accept(MediaType.APPLICATION_JSON).get(String.class));

to System.out.println(service.path("rs/").path("account/details/1").accept(MediaType.TEXT_PLAIN).get(String.class));

You are only producing TEXT_PLAIN, but you request the media-type APPLICATION_JSON (via accept header), this is why you get the response, that the request is not acceptable.

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