Controller 是 Spring 中最基本的组件,主要处理用户交互,一般每个业务逻辑都会有一个 Controller,供用户请求接口进行数据访问;@RequestMapping 注解用于绑定URI到具体处理器。二者相辅相成,共同完成前后端数据交互。
一、简介
IntelliJ IDEA version:2018.3 Spring Boot version: 2.1.4.RELEASE; Java version:1.8。
Controller 是 Spring 中最基本的组件,主要处理用户交互,一般每个业务逻辑都会有一个 Controller,供用户请求接口进行数据访问。
@Controller @RequestMapping("/user") public class UserController { private static Logger logger = LoggerFactory.getLogger(UserController.class); /** * 示例地址 http://localhost:8087/user/viewUser?ownerId=100 * * @author Wiener * @date 2019/5/8 11:27 */ @RequestMapping("/viewUser") @ResponseBody public User viewUser(Long ownerId) { logger.info("请求参数 ownerId = " + ownerId); User user = new User(); user.setId(ownerId); user.setName(" --> Lucy"); return user; } } // 定义User Bean
public class User { private Long id; private String name; // omit getter、setter and toString
}
在上述示例中,@Controller 是标记在类UserController上面的,所以此类就是一个控制器类对象了。使用@RequestMapping("/user")标记控制器,用@RequestMapping(“/viewUser”) 标记方法,表示当请求“/user/viewUser”的时候访问的是UserController的viewUser方法,它返回了一个User 对象。这些在下文将会详细介绍。
二、@RestController介绍
三、@RequestMapping 配置URI映射
@Target({ElementType.METHOD, ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Documented @Mapping public @interface RequestMapping { String name() default ""; String[] value() default {}; String[] path() default {}; RequestMethod[] method() default {}; String[] params() default {}; String[] headers() default {}; String[] consumes() default {}; String[] produces() default {}; }
注解@Target有两个属性,分别为 ElementType.METHOD 和 ElementType.TYPE,这表明@RequestMapping注解可以添加在类级别和方法级别上。
四、@RequestMapping属性介绍
-
value属性
@RestController@RequestMapping("/home") public class IndexController { @RequestMapping(value = { "", "/page", "page*", "view/*,**/msg" }) String indexMultipleMapping() { return "Hello from index multiple mapping."; } }
前面这段代码中,如下的这些 URI 都会由 indexMultipleMapping() 来处理:
localhost:8080/home localhost:8080/home/page localhost:8080/home/pageabc localhost:8080/home/view/ localhost:8080/home/view/view localhost:8080/home/view2/msg
这个示例同时说明了可以将多个请求映射到一个方法上去,只需要添加一个带有请求路径值列表的 @RequestMapping 注解就行了。
-
params属性
@RequestMapping (value= "/testParams" , params={ "param1=value1" , "param2" , "!param3" }) public String testParams() { System. out .println( "test Params..........." ); return "testParams" ; }
在上面的代码中,我们用@RequestMapping 的params 属性指定了三个参数,这些参数都是针对请求参数而言的,它们分别表示参数param1 的值必须等于value1,param2 必须存在,值无所谓,param3 必须不存在,只有当请求“/testParams”并且满足指定的三个参数条件的时候才能访问到该方法。所以当请求/testParams?param1=value1¶m2=value2 的时候testParams函数能够正确响应;当请求/testParams?param1=value1¶m2=value2¶m3=value3 的时候就无响应,因为在@RequestMapping 的params 参数里面指定了参数param3 是不能存在的。
-
method属性
@RequestMapping (value= "testMethod" , method={RequestMethod. GET , RequestMethod. DELETE }) public String testMethod() { return "method" ; }
在上面的代码中就使用method 参数限制请求方式,以GET 或DELETE 方法请求/testMethod 的时候才能访问testMethod 方法。
-
headers属性
@RequestMapping (value= "testHeaders" , headers={ "host=localhost" , "Accept" }) public String testHeaders() { return "headers" ; }
在上面的代码中当请求/testHeaders 的时候只有当请求头包含Accept 信息,且请求的host 为localhost 的时候才能正确的访问到testHeaders 方法。
-
produces和consumes属性
@PostMapping(value = "/cons", consumes = { "application/JSON", "application/XML" }) public User getConsumes(Long ownerId) { User user = new User(); user.setId(ownerId); user.setName("Consumes attribute --> Lucy"); return user; }