1、@Controller
@Controller注解在类上,表明这个类是一个Spring MVC里的Controller,将其声明成Spring的一个Bean,Dispatcher Servlet会自动扫描注解了此注解的类,并将Web请求映射到注解了@RequestMapping的方法上。在声明普通Bean的时候,使用@Component、@Service、@Repository和@Controller是等同的,因为@Service、@Repository和@Controller都组合了@Component元注解;但在Spring MVC声明控制器Bean的视乎,只能使用@Controller。
2、@RequestMapping
@RequestMapping注解式用来映射Web请求(访问路径和参数)、处理类和方法的。@RequestMapping可以注解在方法上或者类上。注解在方法上的@RequestMapping路径会继承注解在类上的路径,@RequestMapping支持Servlet和request和response作为参数,也支持对request和response的媒体类型进行配置。
3、@ResponseBody
@ResponseBody支持将返回值放在response体内,而不是返回一个页面。我们在很多基于ajax的程序的时候,可以以此注解返回数据而不是页面;此注解可以放在返回值前或者方法上。
4、@RequestBody
@RequestBody允许request的参数在request体中,而不是直接在连接地址后面。此注解放置在参数前。
5、@PathVariable
@PathVariable用来接收路径参数,如/news/001,可接受001作为参数,此注解放置在参数前。
6、@RestController
@ResetController是一个组合注解,组合了@Controller和@ResponseBody,当你只开发一个和页面交互数据的控制器的时候,就可以使用此注解。如果不使用此注解则需要在代码中使用@Controller和@ResponseBody注解。
7、示例
package com.chenfeng.xiaolyuh.controller;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.chenfeng.xiaolyuh.entity.DemoObj;
@Controller // 声明此类是一个控制器
@RequestMapping("/anno") // 映射此类访问路径是/anno
public class DemoAnnoController {
// 此方法没有标注路径,因此使用类级别的路径/auno,produces可以制订返回的response的媒体类型和字符集,
// 或需返回值是json对象,则设置produces = "application/json;charset=UTF-8"
@RequestMapping(produces = {"text/plain;charset=UTF-8", "application/json;charset=UTF-8"})
public @ResponseBody String index(HttpServletRequest request) {
return "url:" + request.getRequestURL() + " can access";
}
@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;
}
@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;
}
@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();
}
// 映射不用的路径到同一方法
@RequestMapping(value = { "/name1", "/name2" }, produces = "text/plain;charset=UTF-8")
public @ResponseBody String remove(HttpServletRequest request) {
return "url:" + request.getRequestURL() + " can access";
}
}
package com.chenfeng.xiaolyuh.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.chenfeng.xiaolyuh.entity.DemoObj;
@RestController // 声明是一个控制器,并且返回时不需要@ResponseBody
@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");
}
}