当使用 ResponseEntity<T>和 @RestController 用于 Spring RESTful 应用程序 [英] When use ResponseEntity<T> and @RestController for Spring RESTful applications

查看:58
本文介绍了当使用 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

例如:

@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 不是很灵活.它标记了整个方法,因此您必须确保您的处理程序方法将始终以相同的方式运行.而且您仍然无法设置标题.您需要 HttpServletResponse.

@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.

基本上,ResponseEntity 可以让您做更多的事情.

Basically, ResponseEntity lets you do more.

这篇关于当使用 ResponseEntity&lt;T&gt;和 @RestController 用于 Spring RESTful 应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆