英文:
RestTemplate try catch
问题
以下是您要求的翻译内容:
我正在使用以下Java(Spring 2.0)代码从web服务中读取一个ResponseEntity:
public ResponseEntity<?> getTemplate() {
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<?> myResponse = restTemplate.postForEntity(
myUrl,
myRequestObj,
MyResponseObj.class);
return myResponse;
}
然而,如果myUrl webservice返回HttpStatus.BAD_REQUEST(400),这不会被赋值给myResponse,并且会抛出错误,因此没有ResponseBody,我需要将请求包装在try catch块中。这样做是否正确,还是有其他方法可以解决?
另外,这是否意味着myUrl webservice不应该有意(通过编程方式)将myResponseObj的HttpStatus设置为HttpStatus.BAD_REQUEST?所以,即使myRequestObj包含错误数据,myUrl webService仍应将响应状态设置为200系列中的某个值,比如HttpStatus.NO_CONTENT。对于如何正确实现此操作,欢迎提供任何评论。
英文:
I'm reading a ResponseEntity from a webService using the Java (Spring 2.0) code below:
public ResponseEntity<?> getTemplate() {
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<?> myResponse= restTemplate.postForEntity(
myUrl,
myRequestObj,
MyResponseObj.class);
return myResponse;
}
However, if the myUrl webservice returns HttpStatus.BAD_REQUEST (400) , this is not assigned to myResponse and an error is thrown, so there is no ResponseBody and I need to wrap the request in a try catch block. Is this correct or is there a way round this?
Also, does this mean that the myUrl webservice should never intentionally (programatically) set the HttpStatus of myResponseObj to HttpStatus.BAD_REQUEST? So,even if myRequestObj contains bad data the myUrl webService should still set the response status to something in the 200's ie HttpStatus.NO_CONTENT. Any comments welcome on how to do this correctly.
答案1
得分: 0
> 这正确吗,还是有其他方法可以解决这个问题?
你描述的行为由 Spring 的默认错误处理程序定义,它会在 400-499 范围内的状态码时抛出 HttpClientErrorException
,在 500-599 范围内的状态码时抛出 HttpServerErrorException
,如果状态码是未知的,则抛出 UnknownHttpStatusCodeException
。要处理这些错误码,你可以捕捉这些异常或者注册一个自定义的异常处理程序,可以参考这里。
> 另外,这是否意味着 myUrl 的 Web 服务不应该故意(以编程方式)将 myResponseObj 的 HttpStatus 设置为 HttpStatus.BAD_REQUEST?
根据 RFC 7231,状态码 400 用于指示服务器由于客户端在创建请求时出错(例如,格式错误的请求语法、无效的请求消息格式或欺骗性的请求路由)而无法处理请求。因此,你可以自由地使用该状态码来向客户端指示这种行为。
英文:
> Is this correct or is there a way round this?
The behaviour you describe is defined by spring's detault error handler, which throws an HttpClientErrorException
in case of a status code in the 400-499 range, an HttpServerErrorException
in case of a status code in the 500-599 range and an UnknownHttpStatusCodeException
if the status code is unknown. To handle such error codes you can either catch the exceptions or register a custom exception handler as described here.
> Also, does this mean that the myUrl webservice should never intentionally (programatically) set the HttpStatus of myResponseObj to HttpStatus.BAD_REQUEST?
According to RFC 7231, the status code 400 is used to indicate that the server can't or won't process the request because of an error the client made creating the request (e.g., malformed request syntax, invalid request
message framing, or deceptive request routing). Therefore you are free to use that status code to indicate such behaviour to the client.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论