HttpClient与Spring Boot之间的json数据交互
使用HttpClient传递key/value数据
HttpClient网络请求代码
HttpClient发送key/value数据,返回json格式数据
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| @Test public void test() { String url = "http://localhost:8080/api/user"; HttpPost httppost = new HttpPost(url); HttpClient httpClient = new DefaultHttpClient(); List<NameValuePair> params = new ArrayList<NameValuePair>(); params.add(new BasicNameValuePair("name", "changyuan")); params.add(new BasicNameValuePair("age", "18")); try { UrlEncodedFormEntity urlEntity = new UrlEncodedFormEntity(params, HTTP.UTF_8); httppost.setEntity(urlEntity); HttpResponse response = httpClient.execute(httppost); if (response.getStatusLine().getStatusCode() == 200) { String result = EntityUtils.toString(response.getEntity()); log.info("result>>>>> " + result); } } catch (Exception e) { e.printStackTrace(); } }
|
后台代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| @RestController public class UserRestController { private static Logger log = Logger.getLogger(UserRestController.class); @Autowired private UserService userService; @PostMapping(value = "/api/user") public User findUserByName(@RequestParam(value = "name") String name, @RequestParam(value = "age") Integer age) { log.info("name>>>>>>>>" + name + " age>>>>>>>>" + age); return userService.findByName(name); } }
|
代码说明
@RestController注解是用来作用所有的Controller层返回json数据,这个注解是Spring4+才有的,我这里使用Spring Boot 1.5.1.RELEASE版本,已经包含了Spring 4.3.6版本
1 2 3 4 5
| <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.1.RELEASE</version> </parent>
|
运行结果
1 2 3 4 5 6 7 8
| D:\java\jdk\bin\java... 17:36:12.329 [main] DEBUG org.apache.http.impl.conn.BasicClientConnectionManager - Get connection for route {}->http: 17:36:12.379 [main] DEBUG org.apache.http.impl.conn.DefaultClientConnectionOperator - Connecting to localhost:8080 17:36:12.479 [main] DEBUG org.apache.http.impl.conn.BasicClientConnectionManager - Connection can be kept alive indefinitely 17:36:12.479 [main] INFO org.spring.springboot.HttpClientTest - result>>>>> {"id":1,"userName":"常远","description":"北京工作","city":{"id":1,"provinceId":1,"cityName":"北京","description":"北京工作"},"work":{"id":1,"workAddress":"北京","company":"北京","name":"常远"}} Process finished with exit code 0
|
使用HttpClient传递json数据
HttpClient网络请求代码
HttpClient发送json数据,返回json格式数据
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| @Test public void testJson() { String url = "http://localhost:8080/api/user"; HttpPost httppost = new HttpPost(url); HttpClient httpClient = new DefaultHttpClient(); try { String json = "{\"id\": \"1\",\"workAddress\": \"北京\",\"company\": \"北京\",\"name\": \"常远\"}"; StringEntity stringEntity = new StringEntity(json,"UTF-8"); stringEntity.setContentType("application/json"); httppost.setEntity(stringEntity); HttpResponse response = httpClient.execute(httppost); if (response.getStatusLine().getStatusCode() == 200) { String result = EntityUtils.toString(response.getEntity()); log.info("result>>>>> " + result); } } catch (Exception e) { e.printStackTrace(); } }
|
后台代码
1 2 3 4 5 6 7 8
| @PostMapping(value = "/api/user", consumes = "application/json;charset=utf-8", produces = "application/json;charset=utf-8") public User findUserByName(@RequestBody Work work) { log.info("work>>>>> " + work); return userService.findByName(""); }
|
代码说明
@PostMapping注解中的属性含义
1.consumes的作用是指定请求数据的格式类型,可以同时指定数据格式类型以及字符编码
2.produces的作用是指定返回值类型,不但可以设置返回值类型还可以设定返回值的字符编
consumes和produces属性只是增加对请求数据格式和响应数据格式的校验
运行结果
1 2 3 4 5 6 7 8 9 10
| D:\java\jdk\bin\java... 17:39:52.636 [main] DEBUG org.apache.http.headers - >> Content-Length: 72 17:39:52.636 [main] DEBUG org.apache.http.headers - >> Content-Type: application/json 17:39:52.636 [main] DEBUG org.apache.http.headers - >> Host: localhost:8080 17:39:52.636 [main] DEBUG org.apache.http.headers - >> Connection: Keep-Alive 17:39:53.836 [main] DEBUG org.apache.http.impl.conn.BasicClientConnectionManager - Connection can be kept alive indefinitely 17:39:53.836 [main] INFO org.spring.springboot.HttpClientTest - result>>>>> {"id":1,"userName":"常远","description":"北京工作","city":{"id":1,"provinceId":1,"cityName":"北京","description":"北京工作"},"work":{"id":1,"workAddress":"北京","company":"北京","name":"常远"}} Process finished with exit code 0
|
博客中若有错误,欢迎大家指出噢,大家共同学习,共同提高,嘿嘿
分享
新浪微博
QQ空间
QQ好友
豆瓣
Facebook
取消