http.web restTemplate
http请求
@RequestBody需要把所有请求参数作为json解析,因此,不能包含key=value这样的写法在请求url中,所有的请求参数都是一个json
1,setContentType
Content-Type代表发送端(客户端|服务器)发送的实体数据的数据类型
2,setAccept
accept表示 客服端(浏览器)支持的类型,也是希望服务器响应发送回来的的数据类型。
@Bean public RestTemplate restTemplate(){ RestTemplate rt = new RestTemplate(); SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory(); //60s requestFactory.setConnectTimeout(60*1000); requestFactory.setReadTimeout(60*1000); rt.setRequestFactory(requestFactory); return rt; @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"); 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(@RequestBody Map<String,String> request){ //提供者如此修改,可接收name public TestReponse myTestHttp(@RequestBody TestRequest request){ //提供者如此修改,可接收name hh.add("Authorization","");hh.setContentType(MediaType.APPLICATION_JSON); hh.setContentType(new MediaType("application","json", Charset.forName("UTF-8"))); hh.setAccept(Arrays.asList(new MediaType("application","json", Charset.forName("UTF-8")))); Map maps = new HashMap<String,String>(); maps.put("name","maness"); HttpEntity<Map> he = new HttpEntity<Map>(maps,hh); ResponseEntity<String> ex = rest.exchange("http://localhost:9093/myTestHttp", HttpMethod.POST,he,String.class); 提供者 public TestReponse myTestHttp(@RequestBody Map<String,String> request){ //提供者如此修改,可接收name public TestReponse myTestHttp(@RequestBody TestRequest request){ //提供者如此修改,可接收name hh.setContentType(new MediaType("application","x-www-form-urlencoded", Charset.forName("UTF-8"))); hh.setAccept(Arrays.asList(new MediaType("application","json", Charset.forName("UTF-8")))); MultiValueMap<String, Object> postParameters = new LinkedMultiValueMap<>(); postParameters.add("name", "xxxxxnae"); HttpEntity<MultiValueMap<String, Object>> he = new HttpEntity<>(postParameters, hh); ResponseEntity<String> ex = rest.exchange("http://localhost:9093/myTestHttp", HttpMethod.POST,he,String.class); 提供者 public TestReponse myTestHttp(TestRequest request){ 接收 public TestReponse myTestHttp(String name,String age){ 接收
ResponseEntity<MyTest> responseEntity = restTemplate.exchange(url, httpMethod, formHeader, MyTest.class, uriVariable);
@FeignClient(value="myClient",url="http://localhost:9093", fallbackFactory=MyFeignClientFallback.class,configuration = FeignConfiguration.class)// fallback = MyFeignClientFallbackOne.class fallbackFactory=MyFeignClientFallback.class public interface MyFeignClient { // @Headers({"Content-Type: application/x-www-form-urlencoded","Accept: application/json"}) @RequestMapping(value="/myService",method = RequestMethod.GET) TestReponse myFeignTest(Map<String,Object> map);// @RequestMapping(value="/myTestHttp",method = RequestMethod.GET) TestReponse myFeignTest2(@RequestParam("name") String name,@RequestParam("age") String age); // @Headers({"Content-Type: application/x-www-form-urlencoded","Accept: application/json"}) @RequestMapping(value="/myTestHttp",method = RequestMethod.POST) TestReponse myFeignTest3( TestRequest tr); }