天天看點

Jersey Client Post Bean參數

代碼:

public static void main(String[] args) {

            Student st = new Student("Adriana", "Barrer", 12, 9);
            ClientConfig clientConfig = new DefaultClientConfig();
            clientConfig.getFeatures().put(
                    JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);
            Client client = Client.create(clientConfig);
            WebResource webResource = client
                    .resource("http://localhost:8080/JerseyJSONExample/rest/jsonServices/send");
            ClientResponse response = webResource.accept("application/json")
                    .type("application/json").post(ClientResponse.class, st);
            if (response.getStatus() != 200) {
                throw new RuntimeException("Failed : HTTP error code : "
                        + response.getStatus());
            }
            String output = response.getEntity(String.class);
            System.out.println("Server response .... \n");
            System.out.println(output);
    }      

資源中代碼:

@POST
    @Path("/send")
    @Consumes(MediaType.APPLICATION_JSON)
    public Response consumeJSON( Student student ) {
        System.out.println("student :"+student);
        System.out.println("student content:"+student.getFirstName());
        String output = student.toString();

        return Response.status(200).entity(output).build();
    }      

實體bean中必須要有預設的空構造器。

除非注明轉載,其他文章均為作者原創,可以自由轉載,但請注明轉載的本文的位址,請尊重作者的勞動成果。