Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Boot : Request method 'POST' not supported

I am staring working on spring boot and trying a simple Rest Controller. I have two methods using HTTP GET and they work fine. However when I do a HTTP POST it is not working showing : : Request method 'POST' not supported

My Controller code as below:-

enter code here

package com.example.web.api;
import java.math.BigInteger;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.example.model.Greeting;
@RestController
public class GreetingController {

    private static BigInteger nextId;
    private static Map<BigInteger, Greeting> greetingMap;

    private static Greeting save(Greeting greeting){
        if (greetingMap==null){
            greetingMap = new HashMap<BigInteger, Greeting>();
            nextId = BigInteger.ONE;
        }
        greeting.setId(nextId);
        nextId=nextId.add(BigInteger.ONE);
        greetingMap.put(greeting.getId(), greeting);
        return greeting;
    }

    static {
        // First Greeting
        Greeting g1 = new Greeting();
        g1.setText("Hello World!!");
        save(g1);
        // Second Greeting
        Greeting g2 = new Greeting();
        g2.setText("Hola Mundo!!");
        save(g2);

    }

    /*  
     * 
     * Issue a GET to view greetings
     * 
     */
    @RequestMapping(
            value="/api/greetings",
            method=RequestMethod.GET,
            produces=MediaType.APPLICATION_JSON_VALUE
            )
    public ResponseEntity<Collection<Greeting>> getGreetings(){

        Collection<Greeting> greetings=greetingMap.values();
        return new ResponseEntity<Collection<Greeting>>(greetings, HttpStatus.OK);

    }

    /*  
     * 
     * Issue a GET to view single greeting by id value
     * 
     */

    @RequestMapping(
            value="/api/greetings/{id}",
            method=RequestMethod.GET,
            produces=MediaType.APPLICATION_JSON_VALUE
            )
    public ResponseEntity<Greeting> getGreeting(@PathVariable("id") BigInteger id){


        Greeting greeting = greetingMap.get(id);
        if(greeting == null){

            return new ResponseEntity<Greeting>(HttpStatus.NOT_FOUND);
        }

        return new ResponseEntity <Greeting> (greeting, HttpStatus.OK);

            }

    /*  
     * 
     * Create a POST to add a greeting
     * 
     */

    @RequestMapping(
            value="/api/greetings/",
            method=RequestMethod.POST,
            consumes=MediaType.APPLICATION_JSON_VALUE,
            produces=MediaType.APPLICATION_JSON_VALUE
            )
    public ResponseEntity<Greeting> createGreeting(@RequestBody Greeting greeting){



    Greeting savedGreeting = save(greeting);
    return new ResponseEntity <Greeting> (savedGreeting, HttpStatus.CREATED);
    }


    /* End of HTTP Methods */

}


Kindly advise , what is wrong with createGreeting Method.

Kind regards

like image 260
dididxb Avatar asked Nov 19 '15 07:11

dididxb


2 Answers

your POST method has a trailing slash /api/greetings/ which you missed in your curl call and the other thing you missed is a Content-type header. You should say to server what type of data you send.

curl -X POST -d '{"text":"hello"}' -H "Content-type:application/json" http://localhost:8080/api/greetings/ is a working curl call.

like image 186
Nikolay Rusev Avatar answered Oct 13 '22 10:10

Nikolay Rusev


You need to modify the Content-Type to json

example

like image 37
polo Avatar answered Oct 13 '22 10:10

polo