Home > Java > javaTutorial > How to use Java @PostMapping and @GetMapping methods

How to use Java @PostMapping and @GetMapping methods

WBOY
Release: 2023-04-14 23:28:01
forward
1653 people have browsed it

1. Use the post method to call

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 {
}		
Copy after login

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");
//这样也是可以拿到你想要的值的
}
Copy after login

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 {
}
Copy after login

2. Use the get method

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 {
}
Copy after login

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 {
}
Copy after login

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 {
}
Copy after login

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!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template