简体   繁体   English

在 Spring Boot 中处理无效的 PathVariables

[英]Handling invalid PathVariables in Spring Boot

Suppose I have a GET controller /api/product/{id} where id is an integer.假设我有一个 GET 控制器 /api/product/{id} ,其中 id 是一个整数。 What is the best way to handle requests with non-integer ids?处理具有非整数 ID 的请求的最佳方法是什么? At the moment Spring just throws a java.lang.NumberFormatException and returns a 400.目前 Spring 只是抛出一个 java.lang.NumberFormatException 并返回一个 400。

You can have path variable validation, assuming id should be a positive int value.您可以进行路径变量验证,假设 id 应该是正整数值。

@GetMapping("/api/product/{id}")
public void getProduct(@PathVariable("id") @Min(value = 0) Integer id) {
    // ...
}

Read more above request validation here . 在此处阅读以上请求验证的更多信息

You can even try @Range if you know product id has some range of int value.如果您知道产品 ID 具有一定范围的 int 值,您甚至可以尝试@Range

A 400 response seems appropriate given your statement that id is an integer.鉴于您声明id是整数,400 响应似乎合适。

If your objection is to Spring's handling of it, you could always make your path variable a String, do the number check yourself, and silently return a 400 or any other response you want.如果您反对 Spring 对它的处理,您可以始终将路径变量设置为字符串,自己检查数字,然后静默地返回 400 或您想要的任何其他响应。

@GetMapping("/api/product/{id}")
public ResponseEntity<Product> getProduct(@PathVariable("id") String s) {
    try {
       int id = Integer.parseInt(s);
       // valid integer id here, do normal work ...
    }
    catch(NumberFormatException nfe) {
       return ResponseEntity.status(400).build(); // or whatever error code you want
    }
}

Suppose I have a GET controller /api/product/{id} where id is an integer.假设我有一个 GET 控制器 /api/product/{id} ,其中 id 是一个整数。 What is the best way to handle requests with non-integer ids?处理具有非整数 ID 的请求的最佳方法是什么?

If /api/product/abcde is a resource without a current representation, then you should probably return a 404 Not Found , with an explanation of the error in the message body.如果/api/product/abcde是没有当前表示的资源,那么您可能应该返回404 Not Found ,并在消息正文中解释错误

Status codes are meta data, the target audience of a status code are general purpose components that understand the "transfer documents over a network" domain.状态代码是元数据,状态代码的目标受众是理解“通过网络传输文档”域的通用组件。 Here, you want to call attention to the spelling of the target-uri, and 404 is the appropriate way to do that.在这里,您希望引起注意 target-uri 的拼写,而 404 是这样做的合适方法。

410 Gone might be a reasonable alternative to 404. 410 Gone可能是 404 的合理替代。

A 404 status code does not indicate whether this lack of representation is temporary or permanent; 404 状态代码并不表示这种缺乏表示是暂时的还是永久的; the 410 (Gone) status code is preferred over 404 if the origin server knows, presumably through some configurable means, that the condition is likely to be permanent.如果源服务器知道(大概是通过某种可配置的方式)该条件可能是永久性的,则 410(消失)状态代码比 404 更受欢迎。

The 410 response is primarily intended to assist the task of web maintenance by notifying the recipient that the resource is intentionally unavailable and that the server owners desire that remote links to that resource be removed. 410 响应的主要目的是通过通知接收者资源故意不可用以及服务器所有者希望删除到该资源的远程链接来协助 Web 维护任务。 Such an event is common for limited-time, promotional services and for resources belonging to individuals no longer associated with the origin server's site.这种事件对于限时促销服务和属于不再与源服务器站点关联的个人的资源来说很常见。 It is not necessary to mark all permanently unavailable resources as "gone" or to keep the mark for any length of time -- that is left to the discretion of the server owner.没有必要将所有永久不可用的资源标记为“消失”或将标记保留任何时间长度——这由服务器所有者自行决定。

Note that both 404 and 410 are "cacheable by default", which is probably what you want in this case.请注意, 404410都是“默认可缓存的”,这可能是您在这种情况下想要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM