|
|
请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
( e5 \7 q. L6 [9 _! h6 Z
8 x/ ^- m% J7 A4 r' H+ b: Q+ z5 i
@RequestMapping的value属性用于匹配URL映射,value支持简单表达式:
( t' y) D8 A0 }, z: f
, o6 v" H% k' } E* H+ p$ S4 k0 q@RequestMapping注解提供了method参数指定请求的Method类型,包括RequestMethod.GET、RequestMethod.POST、RequestMethod.DELETE、RequestMethod.PUT等值,分别对应HTTP请求的Method( ~) W& p3 S; ]( H+ U) n
3 t9 L8 z z% S8 y) R( s, L; a/ M& P- O- / V/ ]- d' b" f, Z
- @ResTController7 `, f; R- G5 g" D1 ], s0 R6 U
- @RequestMapping("/user")
$ ]! {; O4 N/ I0 n0 l5 z) T - public class UserRestController {
/ Q0 q9 D6 R1 G! W% j6 p - @RequestMapping("/getuser/{id}")3 W3 l/ j( p3 m/ e8 N; ?! M
- //@PathVariable注解作用在方法参数中,用于表示参数的值来自URL路径
2 {. y+ h" B+ W9 t- |# @) }1 ` - public String getUser(@PathVariable("id") Long id)
: P$ |! L y$ z3 X4 d - {
+ z5 U9 }4 Q% l - User user = new User();9 D0 ~/ Y. v! j; r
- user.setFirstname("Donald");
+ ^+ P0 j/ x1 j" y, z9 v - user.setLastname("Xeong");
* T2 Z; ?9 P3 m- V3 L- z - user.setAge(40);. u; J" s8 S1 }' {* M
- return id.toString() + "\t" + user.getAge()+ "\t" + user.getFirstname() + "\t"+ user.getLastname();, y# Y% l1 M6 x. q8 `
- }) W) |+ Y* N6 m) D3 r3 {
6 j5 p/ D9 \4 F& p( |- @RequestMapping(value = "/getData",method = RequestMethod.GET)) V h9 q! Z2 q; M( Q3 I1 S3 D% m
- public String getData() {% {# T- r" v. A1 C& Q9 G
- return "requestMethod Get"; n4 i4 g5 C+ W+ K
- }, h) Q% p$ g C
- @RequestMapping(value = "/postData",method = RequestMethod.POST)
: O$ y/ m4 B) I4 n5 i - public String postData()+ H$ L9 _, R: m) k% t/ t. L
- {
2 W' x: b4 c! ?/ i3 }- P - return "RequestMethod Post";
a4 K9 }- R. \! V, G - }# f7 i+ O( ^" Q% Y+ D D2 f
- }
( [ {/ P* ~0 f2 Q1 w
2 w) W0 v* W5 C+ A( Y6 h0 @( k4 e2 f
复制代码
/ Q, o H6 f/ x J: j' V+ }0 Z5 k/ m* [) g3 M
' \& P' w3 `4 k- A
5 t# c/ N! s& l0 n# G |
|