Is it possible in Spring MVC to have void handlers

2019-09-17 06:52发布

Is it possible in Spring MVC to have void handler for request?

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

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

1条回答
虎瘦雄心在
2楼-- · 2019-09-17 07:17

you can return void, then you have to mark the method with

@ResponseStatus(value = HttpStatus.OK) you don't need @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:

Return void and mark the method with @ResponseStatus(value = HttpStatus.OK)

Return An object and mark it with @ResponseBody

Return an HttpEntity instance

Also refer this for interesting information.

查看更多
登录 后发表回答