1

I am new to spring boot, creating a simple CRUD operations using Rest API. I have written two methods one for GET and POST. Get works fine but when I try to access post method it's not working its shows 404 error.

    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public ResponseEntity<?> addStudent(@RequestBody Employee employee){
        service.saveOrUpdate(employee);
        
        return new ResponseEntity<Object>("added succcessfully", HttpStatus.OK);   
    }

this returns:

{
    
    "status": 404,
    "error": "Not Found",
    "message": "No message available",
    "path": "/api/create"
}

@RestController
@EnableAutoConfiguration
@RequestMapping(value = "/api")
public class EmployeeController{
    
    @Autowired
    EmployeeService service;
    
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public ResponseEntity<?> createEmployee(@RequestBody Employee employee){
         service.saveOrUpdate(employee);
         return ResponseEntity.ok().body("Added");
    }
        
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public ResponseEntity<?> list(){
        List<Employee> employee = service.getAllEmployee();
        
        return new ResponseEntity<List<Employee>>(employee, HttpStatus.OK);
    }
Andrzej Sydor
  • 1,373
  • 4
  • 13
  • 28
Vicky
  • 401
  • 1
  • 5
  • 11
  • Can you please add the full context / controller class? Then please share your full request data (or at least url). I'd recommend to use springdoc / springdoc-ui so you can see your api doc for your controller / request mapping. – Manuel Waltschek Aug 24 '20 at 11:47
  • you must be calling the wrong url. What is the RequestMapping on your controller? What is the RequestMapping on your GET method that is working? Which url are you calling? – Manuel Waltschek Aug 24 '20 at 11:49
  • i have updated the code snippet.. can u help me now? @ManuelWaltschek – Vicky Aug 24 '20 at 11:59
  • What is the URL that you are invoking? are you sure you are invoking POST method and not GET ? – planben Aug 24 '20 at 12:01
  • yes invoking post method only – Vicky Aug 24 '20 at 12:08
  • Please let us know the full URL you are calling. I'd recommend to use curl for testing this: https://curl.haxx.se/download.html https://ec.haxx.se/http/http-post example: curl -d '{json}' -H 'Content-Type: application/json' https://example.com Or if you are using an http client like postman at least copy the full url from the request – Manuel Waltschek Aug 24 '20 at 12:24
  • 1
    this is the url --> http://localhost:8088/api/create i am calling it. In postman i am testing – Vicky Aug 24 '20 at 12:29
  • Are you sure the bean is picked up by the ComponentScanner of Spring. If it is not then that could explain the 404 you are getting. Note that spring by default only scans packages under the class annotated with @SpringBootApplication. – Gerben Jongerius Aug 24 '20 at 12:44
  • sorry for asking this, but are you sure you set request method to `post` in postman? – Hakob Hakobyan Aug 24 '20 at 13:15
  • is the list() method working? just to check if the controller is picked up by Spring – Marc Stroebel Aug 24 '20 at 13:26
  • yes list() method is working fine.@MarcStröbel – Vicky Aug 25 '20 at 06:06
  • Yes making post request in postman@HakobHakobyan – Vicky Aug 25 '20 at 06:06
  • think about making a minimal reproducable example in github and share it with us. – Manuel Waltschek Aug 25 '20 at 16:27

0 Answers0