Controller控制器和@RequestMapping的作用

大家好,我是IT修真院武汉分院第15期的学员,一枚正直纯洁善良的JAVA程序员。今天给大家分享一下,修真院官网JAVA任务2中——Controller控制器和@RequestMapping的作用。


一、控制器定义

控制器提供访问应用程序的行为,通常通过服务接口定义或注解定义两种方法实现。 控制器解析用户的请求并将其转换为一个模型。在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";
    }
}

二、@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";
    }


参考文献
1、https://www.cnblogs.com/kuoAT/p/7121753.html
  • 4
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值