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

cxf-rs client 调用

时间:2016-08-02 22:19:51      阅读:319      评论:0      收藏:0      [点我收藏+]

标签:

org.apache.cxf.jaxrs.client.WebClient

get调用

    @GET
    @Path("/echo/{input}")
    @Produces("text/plain")
    public String ping(@PathParam("input") String input) {
        return input;
    }

 

    @Test
    public void testPing() throws Exception {
        WebClient client = WebClient.create(serverUrl + "/c/echo/SierraTangoNevada");
        Response r = client.accept("text/plain").get();
        assertEquals(Response.Status.OK.getStatusCode(), r.getStatus());
        String value = IOUtils.toString((InputStream)r.getEntity());
        assertEquals("SierraTangoNevada", value);
    }

 

 

post调用

 

    @POST
    @Produces("application/json")
    @Consumes("application/json")
    @Path("/jsonBean")
    public Response modifyJson(InputBean input) {
        input.setVal2(input.getVal1());
        return Response.ok().entity(input).build();
    }

 

 

    @Test
    public void testJsonRoundtrip() throws Exception {
//        List<Object> providers = new ArrayList<Object>();
//        providers.add(new org.codehaus.jackson.jaxrs.JacksonJsonProvider());
        
        List<org.codehaus.jackson.jaxrs.JacksonJsonProvider> provider = Collections.singletonList(new org.codehaus.jackson.jaxrs.JacksonJsonProvider());
        
        InputBean inputBean = new InputBean();
        inputBean.setVal1("Maple");
        WebClient client = WebClient.create(serverUrl + "/c/jsonBean", provider);
        InputBean jsonBean = client.accept(MediaType.APPLICATION_JSON)
            .type(MediaType.APPLICATION_JSON)
            .post(inputBean,InputBean.class);
        System.out.println(jsonBean.toString());
//        assertEquals(Response.Status.OK.getStatusCode(), r.getStatus());
//        MappingJsonFactory factory = new MappingJsonFactory();
//        JsonParser parser = factory.createJsonParser((InputStream)r.getEntity());
//        JsonBean output = parser.readValueAs(JsonBean.class);
//        assertEquals("Maple", output.getVal2());
    }

create

get

WebClient org.apache.cxf.jaxrs.client.WebClient.create(String baseAddress)

post

WebClient org.apache.cxf.jaxrs.client.WebClient.create(String baseAddress, List<?> providers)

List<Object> providers = new ArrayList<Object>();
providers.add(new org.codehaus.jackson.jaxrs.JacksonJsonProvider());
List<org.codehaus.jackson.jaxrs.JacksonJsonProvider> provider = Collections.singletonList(new org.codehaus.jackson.jaxrs.JacksonJsonProvider());

 

设置MIME

// Set HTTP Accept header
// Set HTTP Content-Type header
// javax.ws.rs.core.MediaType

webClient.accept(MediaType.APPLICATION_JSON).type(MediaType.APPLICATION_JSON);

post

public <T> T post(Object body, Class<T> responseClass)

例如:

JsonBean inputBean = new JsonBean();[...]
JsonBean jsonBean = client.accept(MediaType.APPLICATION_JSON)
  .type(MediaType.APPLICATION_JSON)
  .post(inputBean,JsonBean.class);

 

cxf-rs client 调用

标签:

原文地址:http://www.cnblogs.com/zno2/p/4689510.html

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