答案 0 :(得分:74)
从3.1版开始,制作位置的最佳方法是使用UriComponentBuilder
参数并将其设置为返回的ResponseEntity
。 UriComponentBuilder
知道上下文并使用相对路径进行操作:
@RequestMapping(method = RequestMethod.POST)
public ResponseEntity<?> createCustomer(UriComponentsBuilder b) {
UriComponents uriComponents =
b.path("/customers/{id}").buildAndExpand(id);
HttpHeaders headers = new HttpHeaders();
headers.setLocation(uriComponents.toUri());
return new ResponseEntity<Void>(headers, HttpStatus.CREATED);
}
从4.1版开始,你可以缩短它
@RequestMapping(method = RequestMethod.POST)
public ResponseEntity<?> createCustomer(UriComponentsBuilder b) {
UriComponents uriComponents =
b.path("/customers/{id}").buildAndExpand(id);
return ResponseEntity.created(uriComponents.toUri()).build();
}
感谢Dieter Hubau指出这一点。
答案 1 :(得分:15)
以下示例来自spring教程:
.event::after {
background-image: url(https://images2.alphacoders.com/819/819763.jpg);
background-size: cover;
background-position: center;
}
.island::after {
background-image: url(https://s-media-cache-ak0.pinimg.com/originals/3b/91/6b/3b916b8b33b7aa08bf8fb920be2aa536.jpg);
background-size: cover;
background-position: center;
}
.scorched::after {
background-image: url(https://images4.alphacoders.com/819/819770.jpg);
background-size: cover;
background-position: center;
}
.card {
overflow: hidden;
}
.serv {
position: relative;
z-index: 1;
}
.serv::after {
content: '';
position: absolute;
top: 0; left: 0; right: 0; bottom: 0;
z-index: -1;
transition: transform 0.7s;
}
.serv:hover::after {
transform: scale(1.1);
}
请注意以下内容将为您计算上下文路径(URI),以避免代码重复并使您的应用程序更具可移植性:
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container">
<div class="card-deck row">
<div class="col-3">
<!--event serv-->
<div class="card">
<div class="card bg-inverse">
<div class="card-block card-inverse event serv">
<h5>Special event</h5>
<p class="lead">PVP / PVE</p>
<a href="#" class="btn btn-sm btn-secondary">Infos</a>
<a href="#" class="btn btn-sm btn-primary">Mods</a>
</div>
</div>
</div>
</div>
<!--/event serv-->
<div class="col-6">
<!--island serv-->
<div class="card">
<div class="card bg-inverse">
<div class="card-block card-inverse island serv">
<h5>The island</h5>
<p class="lead">PVE x 3</p>
<a href="#" class="btn btn-sm btn-secondary">Infos</a>
<a href="#" class="btn btn-sm btn-primary">Mods</a>
</div>
</div>
</div>
</div>
<!--/island serv-->
<div class="col-3">
<!--scorched serv-->
<div class="card">
<div class="card bg-inverse">
<div class="card-block card-inverse scorched serv">
<h5>Scorched earth</h5>
<p class="lead">PVE x 3</p>
<a href="#" class="btn btn-sm btn-secondary">Infos</a>
<a href="#" class="btn btn-sm btn-primary">Mods</a>
</div>
</div>
</div>
</div>
<!--/scorched serv-->
</div>
</div>
答案 2 :(得分:1)
根据:http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.30
应使用绝对URI:
Location = "Location" ":" absoluteURI
URI 应该正确转义:
答案 3 :(得分:1)
这是一个老问题,但如果你想让Spring真正为你构建URI,那么你就可以做到这一点。
@RestController
@RequestMapping("/api/v1")
class JobsController {
@PostMapping("/jobs")
fun createJob(@RequestParam("use-gpu") useGPU: Boolean?): ResponseEntity<Unit> {
val headers = HttpHeaders()
val jobId = "TBD id"
headers.location =
MvcUriComponentsBuilder
.fromMethodName(JobsController::class.java, "getJob", jobId)
.buildAndExpand(jobId)
.toUri()
return ResponseEntity(headers, HttpStatus.CREATED)
}
@GetMapping("/job/{jobId}")
fun getJob(@PathVariable jobId: String) = mapOf("id" to jobId)
}
在这个例子中(用Kotlin编写但类似于java),基URI是/api/v1
(在类的顶部定义)。使用MvcUriComponentsBuilder.fromMethodName
调用让Spring找出正确的完整URI。 ({}}中添加了MvcUriComponentsBuilder
。
答案 4 :(得分:0)
您的方法看起来很好,但为了保持清洁,您可以将代码置于自定义HandlerInterceptor
内,该代码仅在存在HTTP 201时触发。
有关详细信息,请参阅here。