java HttpClientErrorException: 404 null
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46166049/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
HttpClientErrorException: 404 null
提问by Rajeev Rathor
I try to call the address in the controller using RestTemplate and as a result I want to get OK or NOT FOUND status I do so in this controller
我尝试使用 RestTemplate 在控制器中调用地址,因此我想获得 OK 或 NOT FOUND 状态我在这个控制器中这样做
@GetMapping(value = "/thanks")
public ModelAndView confirmAccount(
@RequestParam String token,
UriComponentsBuilder uriComponentsBuilder
) {
RestTemplate restTemplate = new RestTemplate();
HttpEntity<Object> entity = new HttpEntity<>(new HttpHeaders());
UriComponents uriComponents
= uriComponentsBuilder.path("/register/token/{token}").buildAndExpand(token);
ResponseEntity<Boolean> response = restTemplate
.exchange(uriComponents.toUri(),
HttpMethod.PUT,
entity,
Boolean.class);
return response.getStatusCode().toString().equals("200")
? new ModelAndView("redirect:/signIn") : new ModelAndView("tokenNotFound");
}
I call this address of the controller.
我称之为控制器的这个地址。
@RequestMapping(value = "/register/token/{token}", method = RequestMethod.PUT)
public
HttpEntity<Boolean> confirmAccount(
@PathVariable String token
) {
Optional<User> userOptional = userService.findByActivationToken(token);
if(userOptional.isPresent()) {
User user = userOptional.get();
user.setActivationToken(null);
user.setEnabled(true);
userService.saveUser(user);
} else {
return ResponseEntity.notFound().build();
}
return ResponseEntity.ok(true);
}
As a result, she throws me out in the console
结果她把我扔到控制台
org.springframework.web.client.HttpClientErrorException: 404 null
at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:63) ~[spring-web-4.3.10.RELEASE.jar:4.3.10.RELEASE]
at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:700) ~[spring-web-4.3.10.RELEASE.jar:4.3.10.RELEASE]
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:653) ~[spring-web-4.3.10.RELEASE.jar:4.3.10.RELEASE]
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:628) ~[spring-web-4.3.10.RELEASE.jar:4.3.10.RELEASE]
at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:549) ~[spring-web-4.3.10.RELEASE.jar:4.3.10.RELEASE]
at com.service.app.controller.RegisterController.confirmAccount(RegisterController.java:40) ~[classes/:na]
Why does RestTemplate not want to return the status 404 as a result? enter code here
为什么 RestTemplate 结果不想返回状态 404?在这里输入代码
回答by Rajeev Rathor
I struggled a lot with this exception Even URL is absolutely correct and relative URLs works for POST request. 404 means URL specify in RestTemplate or HttpRequest for GET type method is incorrect due to Query Param or Request Params or Path Params.Reason Query/Path param's value contain special character that converted or interpreted differently. Let Say queryParams is [email protected] which converted into email=test20%xyz41%.com
我在这个异常中挣扎了很多,即使 URL 是绝对正确的,相对 URL 也适用于 POST 请求。404 表示由于查询参数或请求参数或路径参数,在 RestTemplate 或 HttpRequest 中为 GET 类型方法指定的 URL 不正确。原因查询/路径参数的值包含转换或解释不同的特殊字符。假设 queryParams 是 [email protected] 转换成 email=test20%xyz41%.com
Solution: Decoding while passing query/path params.
解决方案:在传递查询/路径参数时进行解码。
Add Following code at server side [controller code]
在服务器端添加以下代码[控制器代码]
ServerSide[Controller]:
服务器端[控制器]:
import java.net.URLDecoder;
import java.net.URLEncoder;
String encodedEmail = URLDecoder.decode(email, "UTF-8");
It perfectly working and tested code.
它完美地工作和测试代码。
回答by Saurabh
a simple explanation can be HttpClientErrorException is unchecked exception. Java docs for this exception tells, 'Exception thrown when an HTTP 4xx is received' ref - https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/client/HttpClientErrorException.html
一个简单的解释可以是 HttpClientErrorException 是未经检查的异常。此异常的 Java 文档告诉“收到 HTTP 4xx 时抛出异常”参考 - https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/client/HttpClientErrorException.html
so try handling exception with appropriate try/catch and throws
所以尝试使用适当的 try/catch 和 throws 处理异常
回答by OAM
In my case, I left out a forward slash at the end of my endpoint and got the error. After putting it on at the end of my url it worked fine.
就我而言,我在端点的末尾省略了一个正斜杠并得到了错误。把它放在我的网址末尾后,它工作正常。