(转)Spring MVC的经常使用注解

@Controller(经常使用) html

注解一个类表示控制器,Spring MVC会自动扫描标注了这个注解的类。ajax

@RequestMapping(经常使用) spring

请求路径映射,能够标注类,也能够是方法,能够指定请求类型,默认不指定为所有接收。其中有8个属性:json

* value:指定请求的实际地址,指定的地址能够是URI Template 模式。浏览器

* method:指定请求的method类型, GET、POST、PUT、DELETE等;  restful

  consumes: 指定处理请求的提交内容类型(Content-Type),例如application/json, text/html;  cookie

  produces:   指定返回的内容类型,仅当request请求头中的(Accept)类型中包含该指定类型才返回;  app

  params: 指定request中必须包含某些参数值是,才让该方法处理。  异步

  headers: 指定request中必须包含某些指定的header值,才能让该方法处理请求。测试

@RequestParam

放在参数前,表示只能接收参数a=b格式的数据,即Content-Type为 application/x-www-form-urlencoded类型的内容。 @RequestBody 放在参数前,表示参数从request body中获取,而不是从地址栏获取,因此这确定是接收一个POST请求的非a=b格式的数据,即Content-Type不为 application/x-www-form-urlencoded类型的内容。

@ResponseBody(经常使用)

放在方法上或者返回类型前,表示此方法返回的数据放在response body里面,而不是跳转页面。通常用于ajax请求,返回json数据。好比异步请求,但愿响应的结果是json数据,那么加上@Responsebody后,就会直接返回json数据。

@RestController

这个是Controller和ResponseBody的组合注解,表示@Controller标识的类里面的全部返回参数都放在response body里面。 @PathVariable 路径绑定变量,用于绑定restful路径上的变量。

@RequestHeader

放在方法参数前,用来获取request header中的参数值。

@CookieValue

放在方法参数前,用来获取request header cookie中的参数值。 GetMapping PostMapping PutMapping.. *Mapping的是Spring4.3加入的新注解,表示特定的请求类型路径映射,而不须要写RequestMethod来指定请求类型。

--------------------------------------------------------------------------------------------------------------------------------    

Controller控制器和@RequestMapping的做用

1、控制器定义
控制器提供访问应用程序的行为,一般经过服务接口定义或注解定义两种方法实现。 控制器解析用户的请求并将其转换为一个模型。在Spring MVC中一个控制器能够包含多个Action(动做、方法)。

使用注解@Controller定义控制器
org.springframework.stereotype.Controller注解类型用于声明Spring类的实例是一个控制器;

Spring可使用扫描机制来找到应用程序中全部基于注解的控制器类,为了保证Spring能找到你的控制器,须要在配置文件中声明组件扫描。

@Controller
@RequestMapping("/foo")
public class FooController {
 
//    @RequestMapping("/action1")
    @RequestMapping({"/","action1","111"})
    public String action1(Model model){
        model.addAttribute("message","测试action1");
        return "foo/index";
    }
}

2、@RequestMapping详解
@RequestMapping注释用于映射url到控制器类或一个特定的处理程序方法。可用于类或方法上。用于类上,表示类中的全部响应请求的方法都是以该地址做为父路径。该注解共有8个属性,注解源码以下:

/**
 * 用于映射url到控制器类或一个特定的处理程序方法.
 */
//该注解只能用于方法或类型上
@Target({ ElementType.METHOD, ElementType.TYPE })
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Mapping
public @interface RequestMapping {
 
    /**
     * 指定映射的名称
     */
    String name() default "";
 
    /**
     * 指定请求的路径映射,指定的地址能够是uri模板,别名为path
     */
    @AliasFor("path")
    String[] value() default {};
 
    /** 别名为value,使用path更加形象
     * 只有用在一个Servlet环境:路径映射URI(例如“/myPath.do”)。
     * Ant风格的路径模式,同时也支持(例如,“/myPath/*.do”)。在方法层面,在主要的映射在类型级别表示相对路径(例如,“edit.do”)
     * 的支持。路径映射的URI可能包含占位符(例如“/$ {}链接”)
     */
    @AliasFor("value")
    String[] path() default {};
 
    /**
     * 指定请求谓词的类型如GET, POST, HEAD, OPTIONS, PUT, PATCH, DELETE, TRACE. 收窄请求范围 The
     * HTTP request methods to map to, narrowing the primary mapping: GET, POST,
     * HEAD, OPTIONS, PUT, PATCH, DELETE, TRACE.
     */
    RequestMethod[] method() default {};
 
    /**
     * 映射请求的参数,收窄请求范围 The parameters of the mapped request, narrowing the
     * primary mapping.
     */
    String[]params() default {};
 
    /**
     * 映射请求头部,收窄请求范围 The headers of the mapped request, narrowing the primary
     * mapping. RequestMapping(value = "/something", headers =
     * "content-type=text/*")
     */
    String[] headers() default {};
 
    /**
     * 指定处理请求的提交内容类型(Content-Type),例如application/json, text/html,收窄请求范围 The
     * consumable media types of the mapped request, narrowing the primary
     * mapping.
     */
    String[] consumes() default {};
 
    /**
     * 指定返回的内容类型,仅当request请求头中的(Accept)类型中包含该指定类型才返回 The producible media types
     * of the mapped request, narrowing the primary mapping. produces =
     * "text/plain" produces = {"text/plain", "application/*"} produces =
     * "application/json; charset=UTF-8"
     */
    String[] produces() default {};
}

value:指定请求的实际地址,指定的地址能够是URI Template 模式(后面将会说明);

@RequestMapping({"/","action1","111"})
// 仅有value元素可省略
    public String action1(Model model){
        model.addAttribute("message","测试action1");
        return "foo/index";
    }


method:指定请求的method类型, GET、POST、PUT、DELETE等;

  @GetMapping("/action5")   //这是一个组合注解等同下面这个注解
//    @RequestMapping(value = "/action5",method = RequestMethod.GET)
    public String action5(Model model){
        model.addAttribute("message","请求谓语动词是GET");
        return "foo/index";
    }

consumes: 指定处理请求的提交内容类型(Content-Type),例如application/json, text/html;

//  请求内容类型必须为text/html
    @RequestMapping(value = "/action6",consumes = "!text/html")
    public String action6(Model model){
        model.addAttribute("message","请求的提交内容类型(Content-Type)是text/html");
        return "foo/index";
    }


produces:    指定返回的内容类型,仅当request请求头中的(Accept)类型中包含该指定类型才返回;

// 客户端接收json且编码为utf-8,多数浏览器Accept设置的为*/*,接收任意类型
    @RequestMapping(value = "/action7",produces="application/json; charset=UTF-8")
    public String action7(Model model) {
        model.addAttribute("message", "客户端能够接收的类型是application/json; charset=UTF-8");
        return "foo/index";
    }


params: 指定request中必须包含某些参数值是,才让该方法处理。

// 请求的参数必须包含id=215与name不等于abc
    @RequestMapping(value = "/action8",params={"id=215","name!=abc"})
    public String action8(Model model) {
        model.addAttribute("message", "请求的参数必须包含id=215与name不等于abc");
        return "foo/index";
    }


headers: 指定request中必须包含某些指定的header值,才能让该方法处理请求。

// 请求头部信息中必须包含Host=localhost:8080     @RequestMapping(value = "/action9",headers="Host=localhost:8080")     public String action9(Model model) {         model.addAttribute("message", "请求头部信息中必须包含Host=localhost:8080");         return "foo/index";     }

相关文章
相关标签/搜索