Detailed notes and property @RequestMapping

 

@RequestMapping notes:

@RequestMapping is one comment Spring Web applications most frequently used. This annotation will be mapped to HTTP request processing method and REST MVC controller.

Usage Request Mapping based 

on Spring MVC application, RequestDispatcher (under Front Controller) responsible for the servlet processing method for routing incoming HTTP requests to the controller. 

When configuring the Spring MVC done, you need to specify the mapping between the request and processing methods. 

To configure a Web mapping request, you will need to spend @RequestMapping comment. 

@RequestMapping annotations may be used at the level of level and / or a method where the controller class. 

Annotation will be at the level of a particular class of request or request mode mapped onto a controller. After you can add another level of annotation methods to further specify the mapping between the processing method. 

The following is an example of a simultaneous application @RequestMapping annotation on the class and method: 

@RestController  
@RequestMapping("/home")  
public class IndexController {  
    @RequestMapping("/")  
    String get() {  
        //mapped to hostname:port/home/  
        return "Hello from get";  
    }  
    @RequestMapping("/index")  
    String index() {  
        //mapped to hostname:port/home/index/  
        return "Hello from index";  
    }  
} 

As shown in the code above, to / home request will be processed by the get () method, and to / home / index request will be handled by the index (). 

@RequestMapping to handle multiple URI 

you can map to a plurality of requests up method, only you need to add annotation @RequestMapping request path list with a value on the line.

@RestController  
@RequestMapping("/home")  
public class IndexController {  
  
    @RequestMapping(value = {  
        "",  
        "/page",  
        "page*",  
        "view/*,**/msg"  
    })  
    String indexMultipleMapping() {  
        return "Hello from index multiple mapping.";  
    }  
}  

In this code, as you can see, @ RequestMapping supports wildcards and ANT-style path. In front of this code, the following URL would be handled by these indexMultipleMapping (): 

localhost:8080/home
localhost:8080/home/
localhost:8080/home/page
localhost:8080/home/pageabc
localhost:8080/home/view/
localhost:8080/home/view/view

  

 

@RequestParam

@RequestMapping with the @RequestParam 

used together with @RequestMapping @RequestParam annotations, with the process parameters of the method parameters may request bound together. 

When used @RequestParam annotation can have a value or no value. This value specifies the parameter needs to be mapped to the request processing method parameter code as follows: 

@RestController  
@RequestMapping("/home")  
public class IndexController {  
  
    @RequestMapping(value = "/id")  
    String getIdByValue(@RequestParam("id") String personId) {  
        System.out.println("ID is " + personId);  
        return "Get ID from query string of URL with value element";  
    }  
    @RequestMapping(value = "/personId")  
    String getId(@RequestParam String personId) {  
        System.out.println("ID is " + personId);  
        return "Get ID from query string of URL without value element";  
    }  
} 

@RequestParam of defaultValue value is used to provide a default value for the parameter to null the request value. 

@RestController  
@RequestMapping("/home")  
public class IndexController {  
    @RequestMapping(value = "/name")  
    String getName(@RequestParam(value = "person", defaultValue = "John") String personName) {  
        return "Required element of request param";  
    }  
} 

In this code, if the person requesting the parameter is empty, then the getName () method of processing the default value will be received John as its argument. 

 

 

@RequestMapping property:

method :

Spring MVC annotation of @RequestMapping processing method capable of HTTP requests, such as GET, PUT, POST, DELETE, and PATCH. 
All requests will be the default HTTP GET type. 

Not mapped to the request not limited in labeling method return value for the request url, a request may be made by agreement of their properties, method such as the request, or a get post. If made of a method defined conditions, even when the request url mapped on, it does not meet the method can not be generated and forwarded to the target physical page view. You need to use the method in @RequestMapping to declare the type of HTTP request method used:

@RestController  
@RequestMapping("/home")  
public class IndexController {  
    @RequestMapping(method = RequestMethod.GET)  
    String get() {  
        return "Hello from get";  
    }  
    @RequestMapping(method = RequestMethod.DELETE)  
    String delete() {  
        return "Hello from delete";  
    }  
    @RequestMapping(method = RequestMethod.POST)  
    String post() {  
        return "Hello from post";  
    }  
    @RequestMapping(method = RequestMethod.PUT)  
    String put() {  
        return "Hello from put";  
    }  
    @RequestMapping(method = RequestMethod.PATCH)  
    String patch() {  
        return "Hello from patch";  
    }  
} 

 

produces:

Its role is to specify the type of the return value, the return value may be set not only the type of encoding can also set the return value of the character;

@Controller  
@RequestMapping(value = "/pets/{petId}", produces="MediaType.APPLICATION_JSON_VALUE"+";charset=utf-8")  
@ResponseBody  
public Pet getPet(@PathVariable String petId, Model model) {      
    // implementation omitted  
} 

 

consumes:

 Submit a request specifying process of the content type (Content-Type), for example, application / json, text / html;

 

@Controller  
@RequestMapping(value = "/pets", method = RequestMethod.POST, consumes="application/json")  
public void addPet(@RequestBody Pet pet, Model model) {      
    // implementation omitted  
}

 

header:

The content request message header narrow range mapping of a request;

 

@Controller
@RequestMapping("/owners/{ownerId}")
public class RelativePathUriTemplateController {

@RequestMapping(value = "/pets", method = RequestMethod.GET, headers="Referer=http://www.ifeng.com/")
  public void findPet(@PathVariable String ownerId, @PathVariable String petId, Model model) {    
    // implementation omitted
  }
}

 

@RestController  
@RequestMapping("/home")  
public class IndexController {  
    @RequestMapping(value = "/head", headers = {  
        "content-type=text/plain"  
    })  
    String post() {  
        return "Mapping applied along with headers";  
    }  
}
@RestController  
@RequestMapping("/home")  
public class IndexController {  
    @RequestMapping(value = "/head", headers = {  
        "content-type=text/plain",  
        "content-type=text/html"  
    }) String post() {  
        return "Mapping applied along with headers";  
    }  
}

 

params :

params element can further help us narrow your request is mapped (defined value of parameter passing, when the value of a defined value, the method enters the running or not running). Use params element, you can make multiple processing methods to process requests for the same URL, and these parameters of the request are not the same. You can define parameters myParams = myValue this format may also use a wildcard to target specific parameter value is not supported in the request.

 

@RestController  
@RequestMapping("/home")  
public class IndexController {  
    @RequestMapping(value = "/fetch", params = {  
        "personId=10"  
    })  
    String getParams(@RequestParam("personId") String id) {  
        return "Fetched parameter using params attribute = " + id;  
    }  
    @RequestMapping(value = "/fetch", params = {  
        "personId=20"  
    })  
    String getParamsDifferent(@RequestParam("personId") String id) {  
        return "Fetched parameter using params attribute = " + id;  
    }  
}

 

 

@RequestMapping shortcut:

Spring 4.3 introduces a variant of the process level annotation, also known as a combination @RequestMapping annotations. Compositions can be better expressed annotation annotated semantic methods. They played the role is for @RequestMapping package, and has become the standard method defined endpoint. 
For example, @ GetMapping is a combination of notes, it is played by a shortcut @RequestMapping (method = RequestMethod.GET) of. 
Level annotations method has the following variants: 

@GetMapping
@PostMapping
@PutMapping
@DeleteMapping
@PatchMapping

The following code shows how to use a combination of notes:

@RestController  
@RequestMapping("/home")  
public class IndexController {  
    @GetMapping("/person")  
    public @ResponseBody ResponseEntity < String > getPerson() {  
        return new ResponseEntity < String > ("Response from GET", HttpStatus.OK);  
    }  
    @GetMapping("/person/{id}")  
    public @ResponseBody ResponseEntity < String > getPersonById(@PathVariable String id) {  
        return new ResponseEntity < String > ("Response from GET with id " + id, HttpStatus.OK);  
    }  
    @PostMapping("/person")  
    public @ResponseBody ResponseEntity < String > postPerson() {  
        return new ResponseEntity < String > ("Response from POST method", HttpStatus.OK);  
    }  
    @PutMapping("/person")  
    public @ResponseBody ResponseEntity < String > putPerson() {  
        return new ResponseEntity < String > ("Response from PUT method", HttpStatus.OK);  
    }  
    @DeleteMapping("/person")  
    public @ResponseBody ResponseEntity < String > deletePerson() {  
        return new ResponseEntity < String > ("Response from DELETE method", HttpStatus.OK);  
    }  
    @PatchMapping("/person")  
    public @ResponseBody ResponseEntity < String > patchPerson() {  
        return new ResponseEntity < String > ("Response from PATCH method", HttpStatus.OK);  
    }  
}  

In this code, each of a processing method uses a combination of variants @RequestMapping annotations. Although each variant can be used with the method @RequestMapping annotation attributes to swap achieve, but a combination of variants is still a best practice - mainly due to a combination of reduced dollar notes on the application you want to configure data, and the code is more readable. 

 

Article reprint to: https://blog.csdn.net/sunshine_yg/article/details/80493604

 

Guess you like

Origin www.cnblogs.com/nhdlb/p/11532643.html