问题描述
我正在使用Spring Framework 4.0.7,MVC和Rest
I am working with Spring Framework 4.0.7, together with MVC and Rest
我可以和平相处:
-
@Controller
-
ResponseEntity<T>
@Controller
ResponseEntity<T>
例如:
@Controller
@RequestMapping("/person")
@Profile("responseentity")
public class PersonRestResponseEntityController {
使用方法(仅用于创建)
With the method (just to create)
@RequestMapping(value="/", method=RequestMethod.POST)
public ResponseEntity<Void> createPerson(@RequestBody Person person, UriComponentsBuilder ucb){
logger.info("PersonRestResponseEntityController - createPerson");
if(person==null)
logger.error("person is null!!!");
else
logger.info("{}", person.toString());
personMapRepository.savePerson(person);
HttpHeaders headers = new HttpHeaders();
headers.add("1", "uno");
//http://localhost:8080/spring-utility/person/1
headers.setLocation(ucb.path("/person/{id}").buildAndExpand(person.getId()).toUri());
return new ResponseEntity<>(headers, HttpStatus.CREATED);
}
退还一些东西
@RequestMapping(value="/{id}", method=RequestMethod.GET)
public ResponseEntity<Person> getPerson(@PathVariable Integer id){
logger.info("PersonRestResponseEntityController - getPerson - id: {}", id);
Person person = personMapRepository.findPerson(id);
return new ResponseEntity<>(person, HttpStatus.FOUND);
}
工作正常
我可以使用做同样的事情:
-
@RestController
(我知道它与@Controller
+@ResponseBody
相同) -
@ResponseStatus
@RestController
(I know it is the same than@Controller
+@ResponseBody
)@ResponseStatus
例如:
@RestController
@RequestMapping("/person")
@Profile("restcontroller")
public class PersonRestController {
使用方法(仅用于创建)
With the method (just to create)
@RequestMapping(value="/", method=RequestMethod.POST)
@ResponseStatus(HttpStatus.CREATED)
public void createPerson(@RequestBody Person person, HttpServletRequest request, HttpServletResponse response){
logger.info("PersonRestController - createPerson");
if(person==null)
logger.error("person is null!!!");
else
logger.info("{}", person.toString());
personMapRepository.savePerson(person);
response.setHeader("1", "uno");
//http://localhost:8080/spring-utility/person/1
response.setHeader("Location", request.getRequestURL().append(person.getId()).toString());
}
退还一些东西
@RequestMapping(value="/{id}", method=RequestMethod.GET)
@ResponseStatus(HttpStatus.FOUND)
public Person getPerson(@PathVariable Integer id){
logger.info("PersonRestController - getPerson - id: {}", id);
Person person = personMapRepository.findPerson(id);
return person;
}
我的问题是:
- 当有充分理由或特定情况时,必须强制使用另一个选项
- 如果(1)无关紧要,则建议使用哪种方法以及为什么.
- when for a solid reason or specific scenario one option must be used mandatorily over the other
- If (1) does not matter, what approach is suggested and why.
推荐答案
ResponseEntity
旨在表示整个HTTP响应.您可以控制其中的所有内容:状态码,标头和正文.
ResponseEntity
is meant to represent the entire HTTP response. You can control anything that goes into it: status code, headers, and body.
@ResponseBody
是HTTP响应正文的标记,而@ResponseStatus
声明HTTP响应的状态代码.
@ResponseBody
is a marker for the HTTP response body and @ResponseStatus
declares the status code of the HTTP response.
@ResponseStatus
不太灵活.它标记了整个方法,因此您必须确保您的处理程序方法将始终以相同的方式运行.而且您仍然无法设置标题.您需要HttpServletResponse
或HttpHeaders
参数.
@ResponseStatus
isn't very flexible. It marks the entire method so you have to be sure that your handler method will always behave the same way. And you still can't set the headers. You'd need the HttpServletResponse
or a HttpHeaders
parameter.
基本上,ResponseEntity
可让您执行更多操作.
Basically, ResponseEntity
lets you do more.
这篇关于当使用ResponseEntity< T>时,和@RestController用于Spring RESTful应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!