SpringMvc常用注解用法

Spring中有两个Controller注解,一个是spring4.0系列中新增的@RestController,一个spring 3中的@Controller注解,首先我们需要了解它们之间有什么差别。
官方文档对@RestController解释:
@RestController is a stereotype annotation that combines @ResponseBody and @Controller.
翻译:@RestController注解是由@Controller和@ResponseBody组合使用。
区别:
1,如果使用@RestController注解在你自定义的Controller类上,视图解析器将不起作用,无法返回JSP页面,返回的内容就是return定义的内容。
2,如果需要返回指定的页面需要使用@Controller,并且使用视图解析器。
3,如果返回Json格式数据,@RestController具备天然的优势,返回其他格式需要设置produces属性的value。如果使用@Controller需要配合@ResponseBody使用。


以下注解介绍基于@RestController进行。

    @RequestMapping

RequestMapping是一个用来处理请求地址映射的注解,可用于类或方法上。用于类上,表示类中的所有响应请求的方法都是以该地址作为父路径。
例如:

    @RestController
    @RequestMapping("/version1.0")
    public class SomtingDemoController{
       
        @RequestMapping("/one")
        public ResponseEntity<String> doSomthingOne(){
            return new ResponseEntity<String>("GET /version1.0/one Success",HttpStatus.OK);
        }
    }


请求到doSomtingOne方法的URL是http://ip:port/version1.0/one
@RequestMapping注解有六个属性:
1, value:设定该方法与URL路径的映射关系。
2, method:指定Http请求的方式,对应Http的四种方法, GET、POST、PUT、DELETE。
3, consumes:指定请求内容的类型(Content-Type),例如 application/json,text/html,默认情况是application/json。
4, produces:指定返回内容的类型,默认情况时application/json。
5, params:指定request中必须包含某些参数值是,才让该方法处理。
8, headers:指定request中必须包含某些指定的header值,才能让该方法处理请求。

其他注解:

    @PathVariable

说明:@PathVariable是用来获得请求url中的动态参数的,动态参数以{变量名}形式呈现,在@RequestMapping映射绑定方法参数中使用@PathVariable绑定动态参数,绑定分为两种情况:
 1)如果动态参数变量名与方法参数名不一致的时候,需要使用@PathVariable(变量名) String 参数名   绑定。
 2)如果动态参数变量名与方法参数名一致的时候,使用@PathVariable String 变量名 即可。
示例:
     @RestController
        @RequestMapping("/version1.0")
        public class SomtingDemoController{
           
            @RequestMapping("/one/{var1}/{var2}/{var3}")
            public ResponseEntity<String> doSomthingOne(@PathVariable("var1") String v1,@PathVariable("var2") String v2,@PathVariable String var3){
                System.out.println("path var1="+var1);
                System.out.println("path var2="+var2);
                return new ResponseEntity<String>("GET /version1.0/one Success",HttpStatus.OK);
            }
        }
 

    @RequestParam

说明:从Request中获取参数值,传统的方式是从HttpServletRequest中获取参数值,写法request.getParameter(参数名)。在SpringMVC中可以使用@RequestParam注解更优雅的将request中参数绑定到方法参数中。
注意:
1),如果直接写@RequestParam String Request参数名或`@RequestParam("Request参数名") String 绑定参数` ,那么如果前端不传或没有Request参数名,则报错,错误信息“HTTP Status 400 - Required String parameter 'aa' is not present”。
2),如果需要配置参数可传可不传,则可以写
  @RequestParam(value="Request参数名",required=false) String 绑定参数名
此时绑定参数名的赋值为null。
其中
  a.required=true 表示必须要传参数,默认情况是true
  b.如果绑定的参数是int类型,例如@RequestParam(value="intVal",required=false) int val,此时required=false,如果前端没有传递intVal参数,那么@RequestParam是将null赋给int,这样会报错“Consider declaring it as object wrapper for the corresponding primitive type”,在int类型绑定的时候,需要使用包装类Integer。

     @RestController
            @RequestMapping("/version1.0")
            public class SomtingDemoController{
                @RequestMapping("/one")
                public ResponseEntity<String> doSomthingOne(
                @RequestParam("param1") String p1,
                @RequestParam String param2,
                @RequestParam(value="intVal",reqired=false) Integer val){
                
                }
            }

@RequestBody
    该注解用于读取Request请求Body中部分数据,使用系统默认配置的HttpMessageConverter进行解析,把响应的数据绑定要返回的对象上,再把返回的数据对象绑定到@RequestBody注解的参数对象上。
    使用场景:
    1)GET和POST提交时,根据request请求header的 Content-Type判断:
     a.application/x-www-form-urlencoded @RequestParam, @ModelAttribute,@RequestBody 都能处理
     b.multipart/form-data 不能处理
     c.application/json, application/xml 必须使用@RequestBody处理。
    2)PUT提交时,同样根据request请求header的 Content-Type判断:
     a.application/x-www-form-urlencoded  必须使用@RequestBody处理
     b. multipart/form-data 不能处理
     c. application/json, application/xml 必须使用@requestBody处理
    
        @RestController
        @RequestMapping("/version1.0")
        public class SomtingDemoController{
           @RequestMapping("/one",method=RequestMethod.POST | GET | PUT)
            public ResponseEntity<String> doSomthingOne(
            @RequestBody UObject obj){
            
            }
        }

猜你喜欢

转载自3051658671.iteye.com/blog/2340892