org.springframework.web.client.HttpClientErrorException:403空(Spring RestTemplate)

时间:2018-12-14 09:51:49

标签: java spring resttemplate

我正在尝试使用RestTemplate发送GET请求。我的代码如下:-

   RestTemplate template = new RestTemplate();
   HttpHeaders headers = new HttpHeaders();
   headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
   headers.add("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36");
   headers.add("_rToken", cookie.getValue());
   String url = getUrl(contextUrl);
   HttpEntity<Object> entity = new HttpEntity<Object>(headers);
   ResponseEntity<String> response = null;
   try {
       response = template.exchange(url, HttpMethod.GET, entity, String.class);
   }
   catch(RestClientException ex) {
       ex.printStackTrace();
   }
   return response;

在调试时,我的URL看起来不错。我作为实体拥有的是:-

body = null,
headers = <{Accept=[application/json], 
            user-agent=[Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36],
           _rToken=[2a6ac90f-1dfb-4df3-8d23-6d8d948fb9b5]}> 

由于我的get方法不带参数,因此body应该为null。我在这里做什么错了?

堆栈跟踪:

  

org.springframework.web.client.HttpClientErrorException:403 null at   org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:94)     在   org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:79)     在   org.springframework.web.client.ResponseErrorHandler.handleError(ResponseErrorHandler.java:63)     在   org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:773)     在   org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:726)     在   org.springframework.web.client.RestTemplate.execute(RestTemplate.java:682)     在   org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:598)     在com.hm.ul.util.PlpUMCommunicator.get(PlpUMCommunicator.java:53)

0 个答案:

没有答案