在SpringMVC的响应中添加Location响应头
背景
HTTP协议有一个状态码是201(Created),表示请求成功、已在服务器上成功创建资源。这是一个非常实用的HTTP状态码。使用这个语义明确的状态码是远比只返回一个HTTP 200要好的。
假设我们发送一个POST请求来创建一本书:
1 | POST /books HTTP/1.1 |
服务器程序在返回201状态码之后,通常还要在响应报文的首部中添加一个Location字段,字段的值为创建资源的URI。响应体中返回新创建好的资源:
1 | HTTP 201 Created |
那么如何在SpringMVC实现呢?解决问题的关键是UriComponentsBuilder和ServletUriComponentsBuilder这两个类,它们俩都成实现这个目标,但稍有不同,后面会提到。
UriComponentsBuilder
使用来自MvcUriComponentsBuilder预先配置好的UriComponentsBuilder
1 |
|
将UriComponentsBuilder注入为方法参数
1 |
|
需要注意的是使用UriComponentsBuilder,将不会计算当前请求路径。您需要手动添加它,也就是如果你只是使用了builder.path("/books/{id}").buildAndExpand(saved.getId()).toUri()
这样的方式,那么Location的值不会包含https://demo.ningyu.me这个前缀。
ServletUriComponentsBuilder
1 |
|
遗留问题
如果是成功创建了多个资源,又该怎么处理呢?
参考
- java - What is the preferred way to specify an HTTP “Location” Response Header in Spring MVC 3? - Stack Overflow https://stackoverflow.com/questions/3318912/what-is-the-preferred-way-to-specify-an-http-location-response-header-in-sprin/15898426
- java - add location header to Spring MVC’s POST response? - Stack Overflow https://stackoverflow.com/questions/42546141/add-location-header-to-spring-mvcs-post-response