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.

Popular General Dev topics Top

New
Exadra37
Please tell us what is your preferred monitor setup for programming(not gaming) and why you have chosen it. Does your monitor have eye p...
New
AstonJ
I really like our #general-developer-forum:in-the-news section and am wondering whether we could automate some of the cross-posting of th...
New
AstonJ
Just been adding some more portals, currently have the following languages: Apache Groovy C C# C++ Clojure CoffeeScript Crystal ...
New
New
mafinar
I always start with excitement and then get busy on 9/10th day. This year, like the year before this, and the year before that, I intend ...
New
New
AstonJ
Things like smart speakers (such Amazon Alexa), smart TVs or other devices with built in microphones, cameras or with other features that...
New
AstonJ
Do we have any digital nomads here? Anyone fancy it? If so, which countries would you consider? I’ve been toying with the idea for a wh...
New
CommunityNews
9 fintech engineering mistakes. Read this list unless you want to build a money dissappearing system
New

Other popular topics Top

AstonJ
What chair do you have while working… and why? Is there a ‘best’ type of chair or working position for developers?
New
ohm
Which, if any, games do you play? On what platform? I just bought (and completed) Minecraft Dungeons for my Nintendo Switch. Other than ...
New
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
SpaceVim seems to be gaining in features and popularity and I just wondered how it compares with SpaceMacs in 2020 - anyone have any thou...
New
AstonJ
There’s a whole world of custom keycaps out there that I didn’t know existed! Check out all of our Keycaps threads here: https://forum....
New
mafinar
Crystal recently reached version 1. I had been following it for awhile but never got to really learn it. Most languages I picked up out o...
New
Margaret
Hello everyone! This thread is to tell you about what authors from The Pragmatic Bookshelf are writing on Medium.
1122 25081 741
New
New
PragmaticBookshelf
Author Spotlight: Karl Stolley @karlstolley Logic! Rhetoric! Prag! Wow, what a combination. In this spotlight, we sit down with Karl ...
New
DevotionGeo
I have always used antique keyboards like Cherry MX 1800 or Cherry MX 8100 and almost always have modified the switches in some way, like...
New