RestTemplate postForObject返回HttpClientErrorException:401 null

Sef*_*efa 2 rest spring resttemplate

我正在尝试向验证服务器发送帖子消息,我正在使用postforobject.当我运行该程序时,我得到"线程中的异常"主"org.springframework.web.client.HttpClientErrorException:401 null"错误

    RestTemplate rest = new RestTemplate();
    String responseEntity;
    String uri = "http://...";
    responseEntity = rest.postForObject(uri, null, String.class);
    System.out.println(responseEntity);
Run Code Online (Sandbox Code Playgroud)

我也试过了

    responseEntity = rest.postForObject(uri, "", String.class);
Run Code Online (Sandbox Code Playgroud)

同样的结果,它没有用.

当我使用Simple Rest Client时,它可以 在此输入图像描述

任何的想法?

Mic*_*hal 6

401 null 可能意味着服务器期望某种形式的身份验证但没有呈现(即请求中没有授权头)

最可能的解释是:简单REST客户端(重新)使用Chrome的cookie.如果您之前使用表单登录对您的服务进行了身份验证,则Simple Rest Client将重用JSESSIONID cookie.要检测到这一点,只需按F12并打开调试器的网络选项卡.仅供参考,还有其他浏览器插件,例如"高级REST客户端",可让您更清楚地了解发送到服务器的内容.

使用restTemplate发送请求时,它无权访问会话cookie(这是预期的).您应该发布到允许匿名访问的URI,或者在请求中提供某种形式的身份验证.如果您需要其他帮助,请发布您的安全配置.