spring of RequestMapping comment

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:
  1. values: the actual address of the requested
  2. method: the method specified request type, GET, POST, PUT, DELETE; all requests will be the default HTTP GET type.
  3. path: and use the same values, the equivalent values.
  4. 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?
  5. headers: The content request message header is reduced, the request map range. The "Accept", "content-type = text / plain", "content-type = text / html"
  6. produces: Specifies the type of the return value, not only the return type of the character set encoding may also return value
  7. 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

Published 68 original articles · won praise 7 · views 2535

Guess you like

Origin blog.csdn.net/Cui6023056/article/details/103913516