Spring注解使用注意点

1 @RestController @Controller
  @RestController注解至关于@ResponseBody + @Controller合在一块儿的做用。
  若是只是使用@RestController注解Controller,则Controller中的方法没法返回jsp页面,配置的视图解析器InternalResourceViewResolver不起做用,返回的内容就是Return 里的内容。
2 @CrossOrigin 跨域注解,spring4
3 @RequestBody @Valid
    入参格式包括application/json, application/xml等,必须用@RequestBody
    使用@Valid验证也没有什么问题,接收参数是json类型,验证对象的数据有效性,@RequestBody+@Valid 才能实现
    eg.
    public Result insert(@RequestBody @Valid EntityClass entity, BindingResult result) {
    {
        if (result.hasErrors()) {
                return ErrorMsgUtil.invalidResult(result);
        }
    }
    public Class EntityClass{
        @NotEmpty(message = "name不能为空")
        private String name;
    }
4 url传参两种类型        
    @RequestMapping(value="/owners/{ownerId}/pets/{petId}/edit", method = RequestMethod.POST)
    public String processSubmit(@ModelAttribute Pet pet) {
       
    }        
    @RequestMapping(value="/owners/{ownerId}/pets/{petId}/edit", method = RequestMethod.POST)
    public String processSubmit(@PathVariable("ownerId"),@PathVariable("petId")) {
       
    }        spring

相关文章
相关标签/搜索