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; }