您的位置:首页 > 编程语言 > Java开发

SpringMvc4.x--Spring MVC的常用注解

2017-07-18 16:48 316 查看
//下列代码显示用到的对象
public class DemoObj {
private Long id;
private String name;

public DemoObj() { //①
super();
}
public DemoObj(Long id, String name) {
super();
this.id = id;
this.name = name;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}


1. @Controller

@Controller
注解在类上,表明这个类是
Spring
MVC
里面的
Controller
,将其声明为
Spring
的一个
Bean
DispatcherServlet
会自动扫描注解了此注解的类,并将请求映射到注解了
@RequestMapping
的方法上,这里特别指出,在声明普通
Bean
的时候,使用
@Component
@Service
@Repository
@Controller
是等同的,因为
@Controller
@Service
@Repository
都组合了
@Component
元注解,但在
Spring
MVC
声明控制器
Bean
的时候,只能使用
@Controller


//[code]@Controller
@Service
@Repository
都组合了
@Component
元注解
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Controller {}[/code]

2. @RequestMapping

@RequestMapping
注解是用来映射
Web
请求(访问路径和参数),处理类和方法的。
@RequestMapping
可注解在类或者方法上。注解在方法上的
@RequestMapping
路径会继承注解在类上的路径,
@RequestMapping
支持
Servlet
request
response
作为参数,也支持对
request
response
的媒体类型进行配置。

3.@ResponseBody

@ResponseBody
支持将返回值放在
response
体内,而不是返回一个页面。我们在很多基于
Ajax
程序的时候,可以以此注解返回数据而不是页面;此注解可放置在返回值前或者方法上。

//可放置在返回值前
//[code]produces
可定制返回的
response
的媒体类型和字符集,或返回值是
json
对象,则设置
porduces="application/json;charset=UTF-8"


@RequestMapping(produces = "text/plain;charset=UTF-8")
public @ResponseBody String index(HttpServletRequest request) {
return "url:" + request.getRequestURL() + " can access";
}

//放置在方法上[/code]
@RequestMapping(value = "/obj", produces = "application/json;charset=UTF-8")
@ResponseBody
public String passObj(DemoObj obj, HttpServletRequest request) {}


4. @RequestBody

@RequestBody
允许
request
的参数在
request
体内,而不是直接链接在地址后面。此注解放置在参数前。

5. @PathVariable

@PathVariable
用来接收路径参数,如
/news/001
,可接收
001
作为参数,此注解放置在参数前。

@RequestMapping(value = "/pathvar/{str}", produces = "text/plain;charset=UTF-8")
public @ResponseBody String demoPathVar(@PathVariable String str, //③
HttpServletRequest request) {
return "url:" + request.getRequestURL() + " can access,str: " + str;
}


6.@RestController

@RestController
是一个组合注解,组合了
@Controller
@ResponseBody
,这就意味着当你只开发一个和页面交互数据的控制的时候,需要使用此注解。若没有此注解,要想实现上述功能,则需要自己在代码中加
@Controller
@ResponseBody
两个注解。

@RestController
@RequestMapping("/rest")
public class DemoRestController {

@RequestMapping(value = "/getjson",produces={"application/json;charset=UTF-8"})
public DemoObj getjson (DemoObj obj){
return new DemoObj(obj.getId()+1, obj.getName()+"yy");
}
@RequestMapping(value = "/getxml", produces={"application/xml;charset=UTF-8"})
public DemoObj getxml(DemoObj obj){
return new DemoObj(obj.getId()+1, obj.getName()+"yy");
}
}


① 使用
@RestController
,声明是控制器,并且返回数据时不需要
@ResponseBody

② 返回数据的媒体类型为
json

③ 直接返回对象,对象会自动转换成
json

④ 返回数据的媒体类型为
xml

⑤ 直接返回对象,对象会自动转换为
xml


代码

常规的
request
参数获取,访问路径为
/requestParam?id=1


@RequestMapping(value = "/requestParam", produces = "text/plain;charset=UTF-8") //⑥
public @ResponseBody String passRequestParam(Long id,
HttpServletRequest request) {

return "url:" + request.getRequestURL() + " can access,id: " + id;
}


演示解释参数到对象,访问路径为
/obj?id=1&name=xx


@RequestMapping(value = "/obj", produces = "application/json;charset=UTF-8")
@ResponseBody
public String passObj(DemoObj obj, HttpServletRequest request) {

return "url:" + request.getRequestURL()
+ " can access, obj id: " + obj.getId()+" obj name:" + obj.getName();

}


演示映射不同的路径到相同的方法,访问路径为
/name1
/name2


@RequestMapping(value = { "/name1", "/name2" }, produces = "text/plain;charset=UTF-8")
public @ResponseBody String remove(HttpServletRequest request) {

return "url:" + request.getRequestURL() + " can access";
}


转自:人生设计师博客http://blog.longjiazuo.com/

github
地址:点击查看
码云地址:点击查看
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: