如何将传入的URI请求绑定到Spring mvc 4中的自定义RequestObject

时间:2016-07-28 02:24:53

标签: spring-mvc

我在tomcat 8上使用Spring 4和JDK 8。

控制器:

@RestController 
public class MyController {
     @RequestMapping(value = "/abc/{id}", method = RequestMethod.GET)
    public ResponseEntity<MyResponse> myMethod(@PathVariable("id") String id,
            @RequestParam(value = "add", required = false, defaultValue = "1") String add,
            @RequestParam(value = "sub", required = false, defaultValue = "1") String sub,
            @RequestParam(value = "mul", required = false, defaultValue = "1") String mul,
            @RequestParam(value = "div", required = false, defaultValue = "1") String div) {

        MyRequest request = new MyRequest();
        request.setId(id);
        request.setAdd(add);
        request.setSub(sub);
        request.setMul(mul);
        request.setDiv(div);

        // call service implementation.. 
        service.myMethodImpl(request);
    }

基本上,我的URI看起来像这样: ?/ ABC / 100添加= 10安培;分= 10安培; MUL = 10安培; DIV = 10

SpringMVC中是否有可用的绑定/ api /注释,因此我不必手动绑定MyRequest?

1 个答案:

答案 0 :(得分:0)

不,

要处理请求,我们只需要那些参数就可以将它们全部设置为POJO,然后使用post方法发送为请求参数,然后spring将自动绑定这些值