当使用ResponseEntity

当使用ResponseEntity

本文介绍了当使用ResponseEntity< T>时,和@RestController用于Spring RESTful应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用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. 有充分理由特定情况时,必须强制使用另一个选项
  2. 如果(1)无关紧要,则建议使用哪种方法以及为什么.
  1. when for a solid reason or specific scenario one option must be used mandatorily over the other
  2. 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不太灵活.它标记了整个方法,因此您必须确保您的处理程序方法将始终以相同的方式运行.而且您仍然无法设置标题.您需要HttpServletResponseHttpHeaders参数.

@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&lt; T&gt;时,和@RestController用于Spring RESTful应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-26 05:57