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

//下列代码显示用到的对象
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的一个BeanDispatcherServlet会自动扫描注解了此注解的类,并将请求映射到注解了@RequestMapping的方法上,这里特别指出,在声明普通Bean的时候,使用@Component@Service@Repository@Controller是等同的,因为@Controller@Service@Repository都组合了@Component元注解,但在Spring MVC声明控制器Bean的时候,只能使用@Controller

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

 

 

2. @RequestMapping


 

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

 

3.@ResponseBody


 

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

//可放置在返回值前
//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"; }

//放置在方法上
    @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地址:点击查看
码云地址:点击查看

posted @ 2017-07-18 16:48  心碎whn  阅读(268)  评论(0编辑  收藏  举报