RESTEasy通过TEXT、XML、JSON进行数据交换
1,TEXT格式:
服务器端代码:
@POST @Path("format/text") @Consumes(MediaType.TEXT_PLAIN) public String text(String input) { System.out.println(input); return input; }
客户端代码:
private static void testFormatText() throws Exception { String url = URL + "body/format/text"; ClientRequest request = new ClientRequest(url); request.body(MediaType.TEXT_PLAIN, "hello world"); String returnItem = request.postTarget(String.class); System.out.println(returnItem); }
2,XML格式:
服务器端代码:
@POST @Path("format/xml") @Produces(MediaType.APPLICATION_XML) @Consumes(MediaType.APPLICATION_XML) public MyCustomer xml(MyCustomer input) { System.out.println(input); return input; }
客户端代码:
private static void testFormatXml() throws Exception { String url = URL + "body/format/xml"; ClientRequest request = new ClientRequest(url); MyCustomer customer = new MyCustomer(); customer.setId(1); customer.setFirstName("Bill"); customer.setLastName("Burke"); customer.setStreet("263 Clarendon Street"); request.body(MediaType.APPLICATION_XML, customer); MyCustomer returnItem = request.postTarget(MyCustomer.class); System.out.println(returnItem); }
3,JSON格式:
服务器端代码:
@POST @Path("format/json") @Produces(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON) public MyCustomer json(MyCustomer input) { System.out.println(input); return input; }
客户端代码:
private static void testFormatJson() throws Exception { String url = URL + "body/format/json"; ClientRequest request = new ClientRequest(url); MyCustomer customer = new MyCustomer(); customer.setId(1); customer.setFirstName("Bill222"); customer.setLastName("Burke222"); customer.setStreet("263 Clarendon Street"); request.body(MediaType.APPLICATION_JSON, customer); MyCustomer returnItem = request.postTarget(MyCustomer.class); System.out.println(returnItem); }
这里注意,在一个方法中不能同时使用Entity对象装载和参数装载方法。所以在代码设计时就要考虑好参数的装载方式。如下代码就会报错:
服务器端:
@GET @POST @Path("format/json/{cid}") @Consumes({MediaType.APPLICATION_JSON,MediaType.MEDIA_TYPE_WILDCARD}) public void allParam(MyCustomer input, @FormParam("author") String author,@QueryParam("id") String idStr,@PathParam("cid") String cidStr, MultivaluedMap<String,String> form) { System.out.println("input:" + input); System.out.println("author:" + author); System.out.println("idStr:" + idStr); System.out.println("cidStr:" + cidStr); System.out.println("form:" + form); }
客户端:
private static void testJsonAndOther()throws Exception { String url = URL + "body/format/json/9999099"; ClientRequest request = new ClientRequest(url); MyCustomer customer = new MyCustomer(); customer.setId(1); customer.setFirstName("Bill222"); customer.setLastName("Burke222"); customer.setStreet("263 Clarendon Street"); request.body(MediaType.APPLICATION_JSON, customer); request.formParameter("author", "Jerval2"); request.formParameter("id", 2321232); request.formParameter("test", "test2"); request.queryParameter("id", 232123); MyCustomer returnItem = request.postTarget(MyCustomer.class); System.out.println(returnItem); }
报错信息:
Exception in thread "main" java.lang.RuntimeException: You cannot send both form parameters and an entity body at org.jboss.resteasy.client.core.executors.ApacheHttpClient4Executor.loadHttpMethod(ApacheHttpClient4Executor.java:194) at org.jboss.resteasy.client.core.executors.ApacheHttpClient4Executor.execute(ApacheHttpClient4Executor.java:107) at org.jboss.resteasy.core.interception.ClientExecutionContextImpl.proceed(ClientExecutionContextImpl.java:39) at org.jboss.resteasy.plugins.interceptors.encoding.AcceptEncodingGZIPInterceptor.execute(AcceptEncodingGZIPInterceptor.java:40) at org.jboss.resteasy.core.interception.ClientExecutionContextImpl.proceed(ClientExecutionContextImpl.java:45) at org.jboss.resteasy.client.ClientRequest.execute(ClientRequest.java:443) at org.jboss.resteasy.client.ClientRequest.httpMethod(ClientRequest.java:677) at org.jboss.resteasy.client.ClientRequest.post(ClientRequest.java:566) at org.jboss.resteasy.client.ClientRequest.post(ClientRequest.java:571) at org.jboss.resteasy.client.ClientRequest.postTarget(ClientRequest.java:578) at com.test.ClientMain.testJsonAndOther(ClientMain.java:98) at com.test.ClientMain.main(ClientMain.java:25)
如下修改为如下,则能正常使用:
服务器端:
@GET @POST @Path("format/json/{cid}") @Consumes({MediaType.APPLICATION_JSON,MediaType.MEDIA_TYPE_WILDCARD}) public void allParam(@FormParam("author") String author,@QueryParam("id") String idStr,@PathParam("cid") String cidStr, MultivaluedMap<String,String> form) { System.out.println("author:" + author); System.out.println("idStr:" + idStr); System.out.println("cidStr:" + cidStr); System.out.println("form:" + form); }
客户端:
private static void testJsonAndOther()throws Exception { String url = URL + "body/format/json/9999099"; ClientRequest request = new ClientRequest(url); request.formParameter("author", "Jerval2"); request.formParameter("id", 2321232); request.formParameter("test", "test2"); request.queryParameter("id", 232123); MyCustomer returnItem = request.postTarget(MyCustomer.class); System.out.println(returnItem); }
结果:
author:Jerval2 idStr:232123 cidStr:9999099 form:{id=[2321232], author=[Jerval2], test=[test2]}