Spring MVC's @requestmapping explanation2013-10-13 23:39 246622 people read review (+) collection report Classification:Java Essentials article (163)
Directory (?) [+]
Introduction:
The rest style was used to develop the program in the previous time, but when the data was submitted in post and put, it was found that the server side did not accept the submitted data (the server-side parameter bindings did not add any annotations), and viewed the submission method as Application/json. And the server side of the data through Request.getreader () does exist in the browser submitted data. In order to find out the reason, the parameter binding (@RequestParam, @RequestBody, @RequestHeader, @PathVariable) was studied, and also looked at the relevant contents of Httpmessageconverter, summarized here.
Brief introduction:
@RequestMapping
Requestmapping is an annotation that handles request address mappings and can be used on classes or methods. On a class, the method that represents all response requests in a class is the parent path of the address.
The requestmapping annotation has six properties, and we'll describe her in three categories below.
1, value, method;
Value: Specifies the actual address of the request, the specified address can be the URI Template mode (which will be explained later);
Method: Specifies the type of method requested, GET, POST, PUT, delete, and so on;
2, Consumes,produces;
Consumes: Specifies the type of submission to process the request (Content-type), such as Application/json, text/html;
Produces: Specifies the type of content returned, only if the specified type is included in the (Accept) type in the request header;
3, Params,headers;
Params: Specifies that some parameter values must be included in the request before the method is processed.
Headers: Specifies that certain header values must be included in the request in order for the method to process requests.
Example: 1, value/method example
Default Requestmapping ("...") that is value;
[Java]View Plaincopy
- @Controller
- @RequestMapping ("/appointments")
- Public class Appointmentscontroller {
- Private final AppointmentBook AppointmentBook;
- @Autowired
- Public Appointmentscontroller (AppointmentBook appointmentbook) {
- This.appointmentbook = AppointmentBook;
- }
- @RequestMapping (method = Requestmethod.get)
- Public map<string, appointment> get () {
- return Appointmentbook.getappointmentsfortoday ();
- }
- @RequestMapping (value="/{day}", method = Requestmethod.get)
- Public map<string, appointment> getforday (@PathVariable @DateTimeFormat (iso=iso. Date) (Date day, model model) {
- return Appointmentbook.getappointmentsforday (day);
- }
- @RequestMapping (value="/new", method = Requestmethod.get)
- Public Appointmentform Getnewform () {
- return new Appointmentform ();
- }
- @RequestMapping (method = Requestmethod.post)
- Public String Add (@Valid appointmentform appointment, bindingresult result) {
- if (result.haserrors ()) {
- return "Appointments/new";
- }
- Appointmentbook.addappointment (appointment);
- return "redirect:/appointments";
- }
- }
Spring Annotation Method