harwind

harwind

Spring Boot Request Mapping: How to Handle Multiple Request Paths in a Controller

I’m working on a Spring Boot project and I have a controller where I want to map multiple request paths to a single method. Let’s say I have a RESTful API for managing products, and I want to handle both GET requests for product details and POST requests for creating new products in the same controller.

Here’s a simplified example of my controller:

@RestController
@RequestMapping("/products")
public class ProductController {

    @GetMapping("/{productId}")
    public ResponseEntity<Product> getProductDetails(@PathVariable Long productId) {
        // Retrieve and return product details
    }

    @PostMapping("/create")
    public ResponseEntity<Product> createProduct(@RequestBody Product newProduct) {
        // Create and return the new product
    }
}

I want to be able to access product details using a GET request to /products/{productId} and create new products with a POST request to /products/create . However, when I try this, the POST request doesn’t seem to work as expected. What am I doing wrong, and how can I make this work with Spring Boot’s request mapping?
I’ve searched through a number of websites without getting an acceptable answer, therefore I’d appreciate advice on how to successfully set up request mappings in a Spring Boot controller to handle various HTTP methods and request pathways.

2 1518 1

Popular General Dev topics Top

AstonJ
Just been adding some more portals, currently have the following languages: Apache Groovy C C# C++ Clojure CoffeeScript Crystal ...
26 1757 24
New
First poster: mafinar
The following languages will help current and new web developers navigate the programming landscape to code web-based services and apps t...
59 1932 25
New
New
Exadra37
I am a Linux user since 2012, more or less, and I always use Ubuntu on my computers, and my last 2 laptops have been used Thinkpads, wher...
64 2913 29
New
DevotionGeo
The version of Java installed with Android Studio on my Mac is the following (when I run java -version) openjdk version "1.8.0_242-relea...
14 1635 10
New
OvermindDL1
What shell(s) do you use, why do you use them, and how do you have them configured? Note, this is about shell’s, not terminals, terminal...
70 2685 32
New
AstonJ
I love my Kindle Oasis for reading, but unfortunately it’s not great for technical books as you often need to click on a link or do a qui...
29 2177 11
New
AstonJ
Saw this on TikTok of all places! :lol: Anyone heard of them before? Lite:
13 3704 5
New
Exadra37
I am thinking in buying one as the second monitor for my Thinkpad while I am travelling: Anyone has experience in using on...
40 2513 17
New
AstonJ
This might be my next keyboard (the down arrow on my Apple Magic Keyboard has stopped working :icon_rolleyes:) https://cdn.shopify.com/s...
9 1642 5
New

Other popular topics Top

AstonJ
If it’s a mechanical keyboard, which switches do you have? Would you recommend it? Why? What will your next keyboard be? Pics always w...
144 8219 50
New
AstonJ
Curious to know which languages and frameworks you’re all thinking about learning next :upside_down_face: Perhaps if there’s enough peop...
236 5509 87
New
AstonJ
I’ve been hearing quite a lot of comments relating to the sound of a keyboard, with one of the most desirable of these called ‘thock’, he...
14 7494 9
New
AstonJ
In case anyone else is wondering why Ruby 3 doesn’t show when you do asdf list-all ruby :man_facepalming: do this first: asdf plugin-upd...
11 5087 5
New
gagan7995
API 4 Path: /user/following/ Method: GET Description: Returns the list of all names of people whom the user follows Response [ { ...
7 2839 4
New
AstonJ
Biggest jackpot ever apparently! :upside_down_face: I don’t (usually) gamble/play the lottery, but working on a program to predict the...
19 2830 11
New
AstonJ
We’ve talked about his book briefly here but it is quickly becoming obsolete - so he’s decided to create a series of 7 podcasts, the firs...
87 5707 50
New
PragmaticBookshelf
Author Spotlight Jamis Buck @jamis This month, we have the pleasure of spotlighting author Jamis Buck, who has written Mazes for Prog...
21 5360 10
New
husaindevelop
Inside our android webview app, we are trying to paste the copied content from another app eg (notes) using navigator.clipboard.readtext ...
1 2859 1
New
AstonJ
This is cool! DEEPSEEK-V3 ON M4 MAC: BLAZING FAST INFERENCE ON APPLE SILICON We just witnessed something incredible: the largest open-s...
0 2334 1
New