java - Spring 启动 : Request method 'POST' not supported

标签 java spring spring-mvc

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

我的 Controller 代码如下:-

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 */

}


请指教,createGreeting 方法有什么问题。

亲切的问候

最佳答案

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

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

关于java - Spring 启动 : Request method 'POST' not supported,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33797968/

相关文章:

来自数据库的 Java JTable 结果

java - Paypal Broadleaf 集成

java - 即使在 SpringMVC 中包含 keystore 证书后也无法对服务器进行身份验证

Java:检查编译时是否存在给定的方法名称

\p{Punct} 上的 Java Regex 帮助

javascript - 如何从数据库中显示所需格式的日期,从iso8601到类型="date"

java - spring/hibernate JSR-303(Bean 验证)本地化消息中的命名参数

Spring boot-RabbitMQ消费者继续打印 "Retrieving delivery for Consumer"

if条件下的javascript表单validation_error

java - 过滤 OCR 结果