Home > Net >  Spring Boot POST getting bad request
Spring Boot POST getting bad request

Time:08-25

I am having 400 bad request error, it is only happening for the post request, I am using @PostMapping. When I send the JSON object using postman it does not work I get the 400 bad request error. Please help if somebody can.

Here is the Controller:

 package com.backend.ShoppingList.controller;

import java.util.ArrayList;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.backend.ShoppingList.model.Store;
import com.backend.ShoppingList.service.StoreService;

@CrossOrigin(origins = "*")
@RestController
@RequestMapping("/")
public class Control {

    @Autowired
    StoreService storeService;


    // create stores
    @PostMapping(path = "/createstore", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<Store> create(@RequestBody Store newStore) throws Exception {
        Store store = storeService.saveStore(newStore);
        if (store == null) {
            throw new Exception();
        } else {
            return new ResponseEntity<>(store, HttpStatus.CREATED);
        }
    }

}

CodePudding user response:

There you can see the postman thing

CodePudding user response:

I think the problem is your path:

you use : https://[domain]/createstore

but your path is : https://[domain]//createstore

Either remove the annotation @RequestMapping("/") or remove the path attribute in the @PostMapping annotation, it depends on other implementation.

  • Related