restTemplate

Posted xingminghui

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了restTemplate相关的知识,希望对你有一定的参考价值。

    http://localhost:9093/myTestHttp?name=1 访问
    @ResponseBody
    public TestReponse myTestHttp(TestRequest request){// name为属性值


@Autowired
@Qualifier("restTemplate")
private RestTemplate rest;

访问者 HttpHeaders hh = new HttpHeaders(); hh.setContentType(new MediaType("application","json", Charset.forName("UTF-8"))); hh.setAccept(Arrays.asList(new MediaType("application","json", Charset.forName("UTF-8")))); // hh.setContentType(MediaType.APPLICATION_JSON_UTF8); JSONObject s = new JSONObject(); s.put("name","wsd"); s.put("age","sd"); //String json = "{‘name‘:‘s‘}"; System.out.println("request:"+s.toString()); HttpEntity<String> he = new HttpEntity<String>(s.toString(),hh); ResponseEntity<String> ex = rest.exchange("http://localhost:9093/myTestHttp", HttpMethod.POST,he,String.class); // ResponseEntity<String> ex = rest.postForEntity("http://localhost:9093/myTestHttp",he,String.class); String res = ex.getBody(); System.out.println(" res: "+res); 提供者 public TestReponse myTestHttp(TestRequest request){// name为属性值 结果: 访问者用json方式请求name值无法传递 public TestReponse myTestHttp(String name,String age){ //提供者如此修改,可接收name 必须有 MappingJackson2HttpMessageConverter @@@待解决 public TestReponse myTestHttp(@RequestBody Map<String,String> request){ //提供者如此修改,可接收name

  

以上是关于restTemplate的主要内容,如果未能解决你的问题,请参考以下文章

RestTemplate

RestTemplate

RestTemplate--解决中文乱码

resttemplate 怎样接受二进制流

笔记:Spring Cloud Ribbon RestTemplate 详解

如何在 RestTemplate 中添加超时