HTTP状态500 - 请求处理失败;嵌套异常是org.springframework.web.client.HttpClientErrorException:401 Unauthorized

时间:2016-04-20 18:54:02

标签: java spring

我想使用Spring RestTemplate向Web服务发出POST请求。

这是我的方法:

paste(
         substr(ls(all.names = TRUE, envir = .GlobalEnv), 1, 3), 
         substr(ls(all.names = TRUE, envir = .GlobalEnv), 4, 5), 
               sep = "-")

[1] "APR-01" "APR-02" "APR-03" "APR-04" "APR-05" "APR-06" "APR-07" "APR-08" "APR-09" "JAN-01" "JAN-02" "JAN-03"
[14] "JAN-04" "JAN-05" "JAN-06" "JAN-07" "JAN-08" "JAN-09" "JAN-10" "JUL-01" "JUL-02" "JUL-03" "JUL-04" "JUL-05" "JUL-06"
[27] "JUL-07" "JUL-08" "JUL-09" "JUL-10" "MAR-10" "MAY-10" "NOV-09" "NOV-10" "OCT-01" "OCT-02" "OCT-03" "OCT-04" "OCT-05"
[40] "OCT-06" "OCT-07" "OCT-08" "SEP-09" "SEP-10"

我得到一个例外说:

public void sendDataToServer(DataEvent dataEvent){

        RestTemplate rt = new RestTemplate();
        rt.getMessageConverters().add(new MappingJackson2HttpMessageConverter());

        String uri = "post URL"; //url is correct but I am hiding it here.

        HttpHeaders headers = new HttpHeaders();
        headers.add("Accept", "application/json");
        headers.setContentType(MediaType.APPLICATION_JSON);

        HttpEntity<String> request = new HttpEntity(dataEvent,headers);

        rt.exchange(uri, HttpMethod.POST, request, Object.class);
    }

当我使用Postman直接向该网址发出请求而不通过我的网络服务时,它可以正常工作,完全不需要授权。

Authorization screenshot

Headers tab

enter image description here

有什么想法吗?

0 个答案:

没有答案