SoFunction
Updated on 2025-03-06

Detailed explanation of the use of Java @PostMapping and @GetMapping methods

1. Call using post method

1. If the front-end passes parameters, if it is an object

For example {id:'1',name:'2222'}

If you receive backend parameters, you need to use @RequestBody ApplyObject applyObject

The entity class is placed behind the requestBody

@PostMapping(value = "/generatedData")
public Result<?> generatedData(@RequestBody ApplyObject applyObject) throws Exception {
}		

If you don't want to use entity classes to receive this package package using JSONObject;

@PostMapping(value = "/generatedData")
public Result&lt;?&gt; generatedData(@RequestBody JSONObject jsonObject) throws Exception {
    String id = ("id");
    String name = ("name");
//This will also get the value you want}

2. If the parameters passed by the front end are a string with a spliced ​​string

For example, xxx/generatedData?id=1&name=222

When receiving parameters in the backend, you need to use @RequestParam("id") String id

@RequestParam The corresponding field name after adding a question mark

@PostMapping(value = "/generatedData")
public Result<?> generatedData( @RequestParam("id") String id, 
								@RequestParam("name") String name) throws Exception {
}

2. Use the get method

1. If the front-end passes parameters, if it is an object

For example {id:'1',name:'2222'}

If the backend parameters are received, there is an entity class placed inside.

@GetMapping(value = "/generatedData")
public Result<?> generatedData(ApplyObject applyObject) throws Exception {
}

2. If the parameters passed by the front end are a string with a spliced ​​string

For example, xxx/generatedData?id=1&name=222

The backend parameter reception requires use. @RequestParam("id") String id

@RequestParam The corresponding field name after adding a question mark

@GetMapping(value = "/generatedData")
public Result<?> generatedData( @RequestParam("id") String id, 
								@RequestParam("name") String name) throws Exception {
}

3. There are placeholders in the path of the front-end passing parameters

For example, xxx/generatedData/id, the specific value placed in the id is placed

If you receive backend parameters, you need to use @PathVariable String id

@GetMapping(value = "/generatedData/{id}")
public Result<?> generatedData(@PathVariable("id") String id) throws Exception {
}

This is the article about the detailed explanation of the Java @PostMapping and @GetMapping methods. For more related Java @PostMapping and @GetMapping content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!