[Posts] Detailed use of address mapping @RequestMapping Detailed map of @RequestMapping Usage Address (rpm) (rpm)

Detailed usage of @RequestMapping address mapping (rpm)

HTTPS: // www.cnblogs.com/qq78292959/p/3760560.html 

article from csdn found then csdn point cnblog 

did not write cnblog above specified source ..

 

introduction:

Some time ago used in the project to develop a RESTful model program, but when the data submitted with POST, PUT mode, can not find the server accepts data submitted (the server parameter binding does not make any notes) to see the submission for the application / json, and the data submitted by the browser play does exist in the data server through request.getReader (). To find out, it would be for parameter binding (@RequestParam, @RequestBody, @RequestHeader, @PathVariable) were studied, and also looked at the content of HttpMessageConverter, summarized in this together.

 

Summary:

@RequestMapping

RequestMapping is a process for annotation request address mapping, it can be used for class or method. For the class, all the methods in response to the request class are represented in the address as the parent path.

RequestMapping notes six attributes, let's put her into three categories described below.

1、 value, method;

value: Specifies the physical address of the request, the address may be specified URI Template mode (will be described later);

method: method specifies the type of request, GET, POST, PUT, DELETE and the like;

 

2、 consumes,produces;

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

produces: content type of the returned request only if the request header (the Accept) containing the specified type will return to type;

 

3、 params,headers;

params: Specifies the request must contain certain parameter values, before allowing the processing method.

headers: Specifies the request must contain certain specified header value, in order for this method to process the request.

 

Example:

1, value / method example

Default RequestMapping ( ".... str ...") is the value of value;

Copy the code
@Controller
@RequestMapping("/appointments")
public class AppointmentsController {

    private 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";
    }
}
Copy the code

The uri value is the following three categories:

A) may be designated as a specific normal value;

B) may be specified as a class value (URI Template Patterns with Path Variables) comprising a variable;

C) can be specified as a class value having a regular expression (URI Template Patterns with Regular Expressions);

 

example B)

@RequestMapping(value="/owners/{ownerId}", method=RequestMethod.GET)
public String findOwner(@PathVariable String ownerId, Model model) {
  Owner owner = ownerService.findOwner(ownerId);  
  model.addAttribute("owner", owner);  
  return "displayOwner"; 
}

example C)

@RequestMapping("/spring-web/{symbolicName:[a-z-]+}-{version:\d\.\d\.\d}.{extension:\.[a-z]}")
  public void handle(@PathVariable String version, @PathVariable String extension) {    
    // ...
  }
}

2 consumes, produces example

cousumes sample:

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

The method of processing only the request Content-Type is "application / json" type of request.

produces sample:

@Controller
@RequestMapping(value = "/pets/{petId}", method = RequestMethod.GET, produces="application/json")
@ResponseBody
public Pet getPet(@PathVariable String petId, Model model) {    
    // implementation omitted
}

The method processes only requests Accept request header contains "application / json" request, while suggesting returned content type is application / json;

 

 

3 params, headers example

params sample:

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

  @RequestMapping(value = "/pets/{petId}", method = RequestMethod.GET, params="myParam=myValue")
  public void findPet(@PathVariable String ownerId, @PathVariable String petId, Model model) {    
    // implementation omitted
  }
}
Copy the code

Only processing request includes called "myParam", the value "myValue" request;

 

 

headers sample:

Copy the code
@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
  }
}
Copy the code

Processing the request header contains only the specified "the Refer" request header and the corresponding value of " http://www.ifeng.com/" of the request;

 

 

 

The above only describes the method RequestMapping specify which requests are processed, the following one will explain how to deal with data (data binding) request submitted and the data returned.

 

 

References:

1、 Spring Web Doc: 

spring-3.1.0/docs/spring-framework-reference/html/mvc.html
 
Category:  the Spring

introduction:

Some time ago used in the project to develop a RESTful model program, but when the data submitted with POST, PUT mode, can not find the server accepts data submitted (the server parameter binding does not make any notes) to see the submission for the application / json, and the data submitted by the browser play does exist in the data server through request.getReader (). To find out, it would be for parameter binding (@RequestParam, @RequestBody, @RequestHeader, @PathVariable) were studied, and also looked at the content of HttpMessageConverter, summarized in this together.

 

Summary:

@RequestMapping

RequestMapping is a process for annotation request address mapping, it can be used for class or method. For the class, all the methods in response to the request class are represented in the address as the parent path.

RequestMapping notes six attributes, let's put her into three categories described below.

1、 value, method;

value: Specifies the physical address of the request, the address may be specified URI Template mode (will be described later);

method: method specifies the type of request, GET, POST, PUT, DELETE and the like;

 

2、 consumes,produces;

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

produces: content type of the returned request only if the request header (the Accept) containing the specified type will return to type;

 

3、 params,headers;

params: Specifies the request must contain certain parameter values, before allowing the processing method.

headers: Specifies the request must contain certain specified header value, in order for this method to process the request.

 

Example:

1, value / method example

Default RequestMapping ( ".... str ...") is the value of value;

Copy the code
@Controller
@RequestMapping("/appointments")
public class AppointmentsController {

    private 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";
    }
}
Copy the code

The uri value is the following three categories:

A) may be designated as a specific normal value;

B) may be specified as a class value (URI Template Patterns with Path Variables) comprising a variable;

C) can be specified as a class value having a regular expression (URI Template Patterns with Regular Expressions);

 

example B)

@RequestMapping(value="/owners/{ownerId}", method=RequestMethod.GET)
public String findOwner(@PathVariable String ownerId, Model model) {
  Owner owner = ownerService.findOwner(ownerId);  
  model.addAttribute("owner", owner);  
  return "displayOwner"; 
}

example C)

@RequestMapping("/spring-web/{symbolicName:[a-z-]+}-{version:\d\.\d\.\d}.{extension:\.[a-z]}")
  public void handle(@PathVariable String version, @PathVariable String extension) {    
    // ...
  }
}

2 consumes, produces example

cousumes sample:

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

The method of processing only the request Content-Type is "application / json" type of request.

produces sample:

@Controller
@RequestMapping(value = "/pets/{petId}", method = RequestMethod.GET, produces="application/json")
@ResponseBody
public Pet getPet(@PathVariable String petId, Model model) {    
    // implementation omitted
}

The method processes only requests Accept request header contains "application / json" request, while suggesting returned content type is application / json;

 

 

3 params, headers example

params sample:

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

  @RequestMapping(value = "/pets/{petId}", method = RequestMethod.GET, params="myParam=myValue")
  public void findPet(@PathVariable String ownerId, @PathVariable String petId, Model model) {    
    // implementation omitted
  }
}
Copy the code

Only processing request includes called "myParam", the value "myValue" request;

 

 

headers sample:

Copy the code
@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
  }
}
Copy the code

Processing the request header contains only the specified "the Refer" request header and the corresponding value of " http://www.ifeng.com/" of the request;

 

 

 

The above only describes the method RequestMapping specify which requests are processed, the following one will explain how to deal with data (data binding) request submitted and the data returned.

 

 

References:

1、 Spring Web Doc: 

spring-3.1.0/docs/spring-framework-reference/html/mvc.html

Guess you like

Origin www.cnblogs.com/jinanxiaolaohu/p/11275791.html