Several methods Spring Boot request parameters, the rear end of the front end interface mode

A, parameter passing method

1. The parameters of the request directly to the parameter written in the corresponding Controller method, url parameters submitted Controller needs and methods consistent with the reference name.

@PostMapping(value = "/cron")
public HttpResult<Boolean> verifyCron(String cronExpress) {
    ....
}

If the "Content-Type" = "application / x-www-form-urlencoded", available post submitted, does not apply to other post request.

url request path: http: // localhost: 8090 / job / cron cronExpress = 0 0 3 * * Hello??

Called the front

request
            .post(`/.../.../cron?cronExpress=${cronExpress}`)
            .set('Content-Type', 'application/x-www-form-urlencoded')

2. are available through HttpServletRequest way, post and get the way.

Front and rear interfaces parametrically

@PostMapping(value = "/getResult")
    public HttpResult getResult(HttpServletRequest request) {
        Map<String, String[]> paramMap = request.getParameterMap();

Called the front

request.post(`/api/service/job/getResult`)
            .set('Content-Type', 'application/x-www-form-urlencoded')
            .send({
                Id : this.state.id,
                status : this.state.chooseState
            })

3. The processing parameters to use bean

Distal request method

//参数
let params = {
            id:values.id,
            branch:values.branch,
            .....
            mailTo: this.state.mailtoSelects.join(",")
}
//请求方式
axios.post(`/service/add`,params).then( res => {
     .......
})

interface

@RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public HttpResult<Boolean> addJob(@RequestBody SceneJob sceneJob) {

4. The acquisition parameters path through @PathVariable

 @GetMapping(value = "/getAll/{Id}")
    public HttpResult allJob(@PathVariable("Id") long id) {
        .....
    }
request
            .get(`/api/service/job/getAll/${this.state.id}`)

The request parameters through the parameter @RequestParam binding method

When there is the request parameter does not exist exception occurs, by setting the properties required = false solution, @ RequestParam (value = "username", required = false)

@RequestMapping(value="/add",method=RequestMethod.GET)
    public String add(@RequestParam("name") String name) {
    ...
}

6. annotation request parameters used to bind @RequestBody into the reference method, a POST request

Second, the difference between @ RequestParm and @RequestBody

Under get requests for comment backstage reception parameters will complain RequestBody, in the post request, notes backstage reception parameters is also being given RequestParam.

RequestParam annotations received from requestHeader parameter, i.e. the request header, i.e. in the url, requestBody annotation is received from requestBody parameter, i.e. the request body.

If the request to get background notes should receive parameters RequestParam, if the request for the post, the background reception parameters annotation is RequestBody.

@RequestParam Get Request Parameter

@PathVaribale get the data in the url

Published 36 original articles · won praise 19 · views 30000 +

Guess you like

Origin blog.csdn.net/qq_27182767/article/details/90209943