请尝试通过REST与Spring一起提交POST到openshift datagrid,并且得到以下信息:
2018-11-19 08:21:47.607错误7297 --- [nio-8080-exec-2] oaccC [。[。[/]。[dispatcherServlet]:Servlet [dispatcherServlet]的Servlet.service()在路径为[]的上下文中引发异常[请求处理失败;嵌套的异常是org.springframework.web.client.HttpClientErrorException:401未经授权],其根本原因是
org.springframework.web.client.HttpClientErrorException:401未经授权
我的代码如下:
@PostMapping("/cachedatagrid")
public ResponseEntity<String> postAccount(@RequestBody Sample samplep) {
RestTemplate restTemplate = new RestTemplate();
String resourceUrl
= "http://myapp/rest/default";
ResponseEntity<Sample> response;
HttpHeaders httpHeaders = this.createHeaders("user", "pwd");
Sample sample = new Sample();
sample.setName("sample");
HttpHeaders httpHeaders2;
httpHeaders2 = restTemplate
.headForHeaders(resourceUrl);
HttpEntity<Sample> requestUpdate = new HttpEntity<Sample>(sample, httpHeaders);
response = restTemplate.postForEntity(resourceUrl, requestUpdate , Sample.class);
return new ResponseEntity<String>("POST Response", HttpStatus.OK);
}
和标题内容:
private HttpHeaders createHeaders(String username, String password){
return new HttpHeaders() {{
String auth = username + ":" + password;
byte[] encodedAuth = Base64.encodeBase64(
auth.getBytes(Charset.forName("US-ASCII")) );
String authHeader = "Basic" + new String(encodedAuth);
set("Authorization", authHeader);
}};
}
我无法找出createHeaders的问题。应该下注授权。
我使用邮递员给该职位打电话: http://localhost:8080/cachedatagrid
我已经将自己的代码与网络上的几个代码进行了比较,而不是为了使其正常工作。
请您对此提供一些指导?我花了几天时间来解决这个问题。
致谢
Érico