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地址:点击查看
码云地址:点击查看
相关文章推荐
- SpringMVC的常用注解
- SpringMVC常用注解,返回方式,路径匹配形式
- spring mvc常用的注解
- springmvc常用注解标签详解
- spring mvc常用的注解
- springmvc常用注解标签
- springMVC注解中@RequestMapping中常用参数value params 以及@RequestParam 详解
- SpringMVC常用注解
- Spring MVC常用的注解
- 记录-SpringMVC4常用注解及详解
- SpringMvc4.x--@ControllerAdvice注解
- springmvc常用注解标签详解
- 详解Spring MVC 4常用的那些注解
- springmvc常用注解标签详解
- springmvc常用注解之@Controller和@RequestMapping
- Spring MVC 常用标签、注解 view、viewResolver 解析
- spring mvc常用注解
- springmvc常用注解
- springmvc常用注解之@Controller和@RequestMapping概要说明
- springMVC常用注解解释