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.

Most Liked

faust

faust

when you say this, what exactly is happening? are you getting some error?

Where Next?

Popular General Dev topics Top

dasdom
No chair. I have a standing desk. This post was split into a dedicated thread from our thread about chairs :slight_smile:
New
AstonJ
Thought it might be worth having a dedicated thread for standing desk treadmills (for those interested, here’s our general thread on stan...
New
New
dwaynebradley
For those that are interested, Snyk (developer security tool) announced support for Elixir earlier this week: Just thought I’d pass it...
New
GermaVinsmoke
Do you like to help others on stackoverflow in your free time? And what’s your reputation on Stackoverflow? :smirk::joy::rofl:
New
Exadra37
A modern streaming platform for mission critical workloads Redpanda is a Kafka® compatible event streaming platform. No Zookeeper®, no JV...
New
Maartz
Hi folks, I don’t know if I saw this here but, here’s a new programming language, called Roc Reminds me a bit of Elm and thus Haskell. ...
New
Exadra37
My brother got a VPS on https://contabo.com hosting provider, but I was not aware of them, and when my brother told me the price and spec...
New
jaeyson
Hi!, hope everyone’s ok. Sorry if this question is ambiguous (i’ll remove this if i break some rules here). This is more like a self-ques...
New
ivanhercaz
Hi! I usually keep changelogs for my projects because I think they are really useful, not only to track the changes and not to be lost b...
New

Other popular topics Top

AstonJ
I ended up cancelling my Moonlander order as I think it’s just going to be a bit too bulky for me. I think the Planck and the Preonic (o...
New
AstonJ
I have seen the keycaps I want - they are due for a group-buy this week but won’t be delivered until October next year!!! :rofl: The Ser...
New
Margaret
Hello content creators! Happy new year. What tech topics do you think will be the focus of 2021? My vote for one topic is ethics in tech...
New
PragmaticBookshelf
“A Mystical Experience” Hero’s Journey with Paolo Perrotta @nusco Ever wonder how authoring books compares to writing articles?...
New
PragmaticBookshelf
Rails 7 completely redefines what it means to produce fantastic user experiences and provides a way to achieve all the benefits of single...
New
AstonJ
If you get Can't find emacs in your PATH when trying to install Doom Emacs on your Mac you… just… need to install Emacs first! :lol: bre...
New
PragmaticBookshelf
Develop, deploy, and debug BEAM applications using BEAMOps: a new paradigm that focuses on scalability, fault tolerance, and owning each ...
New
AstonJ
This is a very quick guide, you just need to: Download LM Studio: https://lmstudio.ai/ Click on search Type DeepSeek, then select the o...
New
AstonJ
Curious what kind of results others are getting, I think actually prefer the 7B model to the 32B model, not only is it faster but the qua...
New
Fl4m3Ph03n1x
Background Lately I am in a quest to find a good quality TTS ai generation tool to run locally in order to create audio for some videos I...
New