当前位置: 首页 > 面试题库 >

当对Spring RESTful应用程序使用ResponseEntity

水品
2023-03-14
问题内容

我正在使用Spring Framework 4.0.7,MVC和Rest

我可以和以下人一起安心工作:

  • @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);
}

to return something

@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
@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());
}

to return something

@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)无关紧要,则建议采用什么方法以及为什么。

问题答案:

ResponseEntity旨在表示整个HTTP响应。你可以控制其中的所有内容:状态码,标头和正文。

@ResponseBody是HTTP响应正文的标记,并@ResponseStatus声明HTTP响应的状态代码。

@ResponseStatus不是很灵活。它标记了整个方法,因此你必须确保你的处理程序方法始终具有相同的行为。而且你仍然无法设置标题。你需要HttpServletResponseHttpHeaders参数。

基本上,ResponseEntity你可以做更多的事情。



 类似资料:
  • null 例如: 方法(只创建) 归还某物 null null

  • 我试图理解我们什么时候需要使用这个应用程序。在我们的node Express中使用 当我在网上搜索时,我在reddit上偶然发现了这个答案,它说明了应用程序之间的区别。获取和应用程序。使用 在此基础上,我总结了以下几点。 充当超级路由或中间件?这意味着它在? 此外,如果有人能添加更多关于app.use.的信息/练习,我将不胜感激

  • 我不能在pyqt应用程序中使用time.sleep,因为这会冻结GUI线程,所以在此期间GUI将完全冻结。我一直在寻找处理这件事的方法。 我试图使用QTimer,但似乎它们需要链接到另一个函数?比如等十秒钟,然后运行一些函数。有没有办法让它等待,然后继续当前的功能?

  • 我是android应用程序开发的新手。我在运行应用程序时遇到了这个问题。 错误:配置项目“:app”时出现问题。 无法解析配置': app:_debugCompile'的所有依赖项。找不到与com.android.support: appcomat-v7:15匹配的任何版本。在以下位置搜索:https://jcenter.bintray.com/com/android/support/appcom

  • 使用npx create react应用程序创建新的react项目时,该过程停止并出现错误:内部/modules/cjs/loader。js:311。 内部/模块/cjs/loader.js:311抛出err;^错误:找不到模块'C:\用户\shonm\桌面\Web开发\WebSites\test1\node_modules\fs-额外\lib\index.js'。请验证package.json是

  • 我正试图通过Appium、Selenium框架自动化一些在Android设备上运行的移动应用程序测试。我已经在Selenium中设置了测试类,并尝试使用以下所需功能启动android驱动程序。我的Appium版本是1.4.16.1 当我在真正的三星设备上运行测试时,应用程序被打开,然后在打开时立即崩溃,并带有消息,我的测试应用程序是 当我在Appium桌面控制台中查看日志时,我看到以下内容: 请帮