ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

java – Spring Boot:不支持请求方法’POST’

2019-06-28 09:05:55  阅读:135  来源: 互联网

标签:java spring-mvc spring


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

我的控制器代码如下: –

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标题.你应该说服务器发送什么类型的数据.

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

标签:java,spring-mvc,spring
来源: https://codeday.me/bug/20190628/1314115.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有