码迷,mamicode.com
首页 > 其他好文 > 详细

Jersey Client Post Bean参数

时间:2014-10-15 19:41:01      阅读:154      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   io   os   ar   sp   div   

代码:

    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中必须要有默认的空构造器。

Jersey Client Post Bean参数

标签:style   blog   http   color   io   os   ar   sp   div   

原文地址:http://www.cnblogs.com/juepei/p/4026981.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!