java Spring Boot:不支持请求方法“POST”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33797968/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 22:08:46  来源:igfitidea点击:

Spring Boot : Request method 'POST' not supported

javaspringspring-mvc

提问by dididxb

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

我正在研究 spring boot 并尝试一个简单的 Rest Controller。我有两种使用 HTTP GET 的方法,它们工作正常。但是,当我执行 HTTP POST 时,它不工作,显示 :: 不支持请求方法“POST”

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.

请告知,createGreeting 方法有什么问题。

Kind regards

亲切的问候

回答by Nikolay Rusev

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-typeheader. You should say to server what type of data you send.

您的 POST 方法有一个尾部斜杠/api/greetings/,您在 curl 调用中错过了该斜线,而您错过的另一件事是Content-type标题。您应该告诉服务器您发送的数据类型。

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

curl -X POST -d '{"text":"hello"}' -H "Content-type:application/json" http://localhost:8080/api/greetings/是一个有效的 curl 调用。

回答by polo

You need to modify the Content-Type to json

需要修改Content-Type为json

example

例子

回答by Saahon

try changing link the value to another, for example:

尝试将值更改为另一个值,例如:

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

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