SpringMVC注解

2018-07-17  本文已影响0人  剑客kb

RequestMapping

@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Mapping
public @interface RequestMapping {
    String name() default "";
    @AliasFor("path")
    String[] value() default {};
    @AliasFor("value")
    String[] path() default {};
    RequestMethod[] method() default {};
    String[] params() default {};
    String[] headers() default {};
    String[] consumes() default {};
    String[] produces() default {};
}

Autowired and Resource

@Autowired
@Qualifier("userDao")

@PathVariable

将请求中的变量映射到方法参数上,同时还对变量支持正则表达式

 @RequestMapping(value = "/sayhello/{type:[a-z-]+}", method = {RequestMethod.GET}, consumes = MediaType.APPLICATION_JSON_VALUE,produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public String say(@PathVariable String type, String word) {
        return type + word;
    }

@RequestParam

功能相当于request.getParameter("name"),有三个属性,value指定映射的字段名称,required=false表示该属性不是必传的,defaultValue可以指定该属性的默认值。当url有俩个word时,会默认把俩个word的value合并成一个String.
当该注解使用在Map<String, String> or MultiValueMap<String, String>这些类型上时,就可以匹配所有的入参

@RequestBody和@ResponseBody

上一篇 下一篇

猜你喜欢

热点阅读