简体   繁体   English

在Spring MVC中是否可以为请求提供无效处理程序?

[英]Is it possible in Spring MVC to have void handlers for requests?

Is it possible in Spring MVC to have void handler for request? 在Spring MVC中是否可能有用于请求的void处理程序?

Suppose I have a simple controller, which doesn't need to interact with any view. 假设我有一个简单的控制器,不需要与任何视图进行交互。

@Controller
@RequestMapping("/cursor")
public class CursorController {

    @RequestMapping(value = "/{id}", method = PUT)
    public void setter(@PathVariable("id") int id) {
        AnswerController.setCursor(id);
    }
}

UPD UPD

@Controller
@RequestMapping("/cursor")
public class CursorController {

    @RequestMapping(value = "/{id}", method = PUT)
    public ResponseEntity<String> update(@PathVariable("id") int id) {
        AnswerController.setCursor(id);
        return new ResponseEntity<String>(HttpStatus.NO_CONTENT);
    }
}

you can return void , then you have to mark the method with 您可以return void ,然后必须将方法标记为

@ResponseStatus(value = HttpStatus.OK) you don't need @ResponseBody @ResponseStatus(value = HttpStatus.OK)您不需要@ResponseBody

@RequestMapping(value = "/updateSomeData" method = RequestMethod.POST)
@ResponseStatus(value = HttpStatus.OK)
public void defaultMethod(...) {
    ...
}

Only get methods return a 200 status code implicity, all others you have do one of three things: 只有get方法隐式地返回200 status code ,所有其他方法都可以做以下三件事之一:

Return void and mark the method with @ResponseStatus(value = HttpStatus.OK) 返回void并使用@ResponseStatus(value = HttpStatus.OK)标记该方法

Return An object and mark it with @ResponseBody 返回一个对象并用@ResponseBody标记它

Return an HttpEntity instance 返回一个HttpEntity实例

Also refer this for interesting information . 另请参阅此以获得有趣的信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM