Home >Java >javaTutorial >How to use Java @PostMapping and @GetMapping methods
1. If the front-end parameter is an object,
such as {id:‘1’,name:‘2222’}
To receive back-end parameters, you need to use @RequestBody ApplyObject applyObject
The requestBody is followed by an entity class
@PostMapping(value = "/generatedData") public Result<?> generatedData(@RequestBody ApplyObject applyObject) throws Exception { }
If you don’t want to use the entity class to receive it, you can use JSONObject to receive this package package com.alibaba.fastjson;
@PostMapping(value = "/generatedData") public Result<?> generatedData(@RequestBody JSONObject jsonObject) throws Exception { String id = jsonObject.getString("id"); String name = jsonObject.getString("name"); //这样也是可以拿到你想要的值的 }
2. If the parameter passed by the front end is a spliced string with ?
such as xxx/generatedData?id=1&name=222
To receive parameters at the back end, you need to use @RequestParam("id") String id
@RequestParam plus the corresponding field name after the question mark
@PostMapping(value = "/generatedData") public Result<?> generatedData( @RequestParam("id") String id, @RequestParam("name") String name) throws Exception { }
1. If the front-end parameter is an object,
such as {id:‘1’,name:‘2222’}
If the back-end parameter is received, the It is an entity class
@GetMapping(value = "/generatedData") public Result<?> generatedData(ApplyObject applyObject) throws Exception { }
2. If the parameter passed by the front end is a spliced string with ?
such as xxx/generatedData?id=1&name=222
To receive parameters at the backend, you need to use @RequestParam("id") String id
@RequestParam plus the corresponding field name after the question mark
@GetMapping(value = "/generatedData") public Result<?> generatedData( @RequestParam("id") String id, @RequestParam("name") String name) throws Exception { }
3. The path of the front-end passed parameters exists Placeholder
such as xxx/generatedData/id, the specific value placed in the id
If you want to receive back-end parameters, you need to use @PathVariable String id
@GetMapping(value = "/generatedData/{id}") public Result<?> generatedData(@PathVariable("id") String id) throws Exception { }
The above is the detailed content of How to use Java @PostMapping and @GetMapping methods. For more information, please follow other related articles on the PHP Chinese website!