2017-11-01 13 views
0

私はSpringプロジェクトに取り組んでいます。無効な応答でヘッダーを送信するにはどうすればよいですか?

私は現在これをやっています。

public ResponseEntity<?> create(@RequestBody final Some entity) { 
    // persist the entity here 
    final URI location = uriComponentsBuilder.path("{id}").buildAndExpand(entity.getId()).toUri(); 
    return ResponseEntity.created(location).build(); 
} 

そして私は@ResponseStatusを見つけました。

@ResponseStatus(HttpStatus.CREATED) 
public void create(@RequestBody @NotNull final BaseType entity) { 
    // persist the entity here 
    // Location header to where? 
} 

このようにしてLocationヘッダーを送信する方法はありますか?

+0

https://docs.spring.io/spring/docs/current/spring-framework-reference/web.html#mvc-ann-return-types –

+0

@JBNizetは 'ResponseEntity 'とにかく動作しますか? –

+0

あなたは自分でそれをテストしてみませんか? –

答えて

2

あなたは以下のような応答エンティティを返すことができます

return new ResponseEntity<>(location, HttpStatus.CREATED); 

または

HttpHeaders headers = new HttpHeaders(); 
headers.add(location); 
return new ResponseEntity<>(headers, HttpStatus.CREATED); 
+0

「ResponseEntity 」はとにかく動作しますか? –

+1

@JinKwonはい –

0

はこれを試してみてください。本文なしであなたの好みのヘッダーとステータスを返します。

@ResponseStatus(HttpStatus.I_AM_A_TEAPOT) 
@RequestMapping("/teapot") 
public HttpHeaders dummyMethod() { 
    HttpHeaders h = new HttpHeaders(); 
    h.add("MyHeader", "MyValue"); 
    return h; 
} 
関連する問題