spring of RequestMapping comment
@RequestMapping is one comment Spring Web applications most frequently used. This annotation will be mapped to HTTP request processing method and REST MVC controller.
RequestMapping classes and methods may be used, all the representations in response to the request address are as follows on the path for the parent class
Must address in order to access / user / RequestMpping access
@Controller
@RequestMapping(path = ("user"))
public class HelloController {
//访问地址可以填多个如:RequestMapping和hello
@RequestMapping( value = {"/RequestMpping","/hello"},params = {"name=zhangsan"},headers ="Accept")//必须传值名字为name 如果name=zhangsan
public String RequestMpping(){
System.out.println("RequestMapping");
return "success";
}
}
RequestMapping There are several attributes:
- values: the actual address of the requested
- method: the method specified request type, GET, POST, PUT, DELETE; all requests will be the default HTTP GET type.
- path: and use the same values, the equivalent values.
- params: When using traditional values requset or access mode, the time of filing of this type must have. Such as: params = { "name = zhangsan"}, must have / RequestMpping name = zhangsan?
- headers: The content request message header is reduced, the request map range. The "Accept", "content-type = text / plain", "content-type = text / html"
- produces: Specifies the type of the return value, not only the return type of the character set encoding may also return value
- consumes: submit a request specifying process content (Content-Type), such as: application / json, text / html;
RequestMapping with the @RequestParam
Binding can pass over the parameters and method parameters with the following code
@RequestMapping( value = {"/RequestMpping","/hello"})
public String RequestMpping(@RequestParam("name") String name){
System.out.println("RequestMapping");
return "success";
}
//如果请求参数和处理方法参数的名称一样的话,@RequestParam 注解的 value 这个参数就可省掉了
@RequestMapping( value = {"/RequestMpping","/hello"},method = {RequestMethod.GET})
public String RequestMpping(@RequestParam(required = false//定义该值不是必须要传的值,
defaultValue = "zhangsan"//当没有值传入时自定义一个值)
String name){
System.out.println(name);
return "success";
}
RequestMapping shortcuts
Spring 4.3 introduces @RequestMapping combination of notes. Compositions can be better expressed annotation annotated semantic methods. They played the role is for @RequestMapping package.
For example, @ GetMapping equivalent @RequestMapping (method = RequestMethod.GET) is a shortcut.
Level annotations method has several variants follows:
@GetMapping
@PostMapping
@PutMapping
@DeleteMapping
@PatchMapping
This article is part from the Internet to collect from, if there is infringement, please contact deleted