Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring Boot:请求方法';邮政';不支持_Java_Spring_Spring Mvc - Fatal编程技术网

Java Spring Boot:请求方法';邮政';不支持

Java Spring Boot:请求方法';邮政';不支持,java,spring,spring-mvc,Java,Spring,Spring Mvc,我开始在spring boot上工作,并尝试一个简单的Rest控制器。 我有两个使用HTTPGET的方法,它们工作得很好。 但是,当我执行HTTP POST时,它不起作用,显示: :不支持请求方法“POST” 我的控制器代码如下:- enter code here package com.example.web.api; import java.math.BigInteger; import java.util.Collection; import java.util.HashMap; imp

我开始在spring boot上工作,并尝试一个简单的Rest控制器。 我有两个使用HTTPGET的方法,它们工作得很好。 但是,当我执行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 */

}
在此处输入代码
包com.example.web.api;
导入java.math.biginger;
导入java.util.Collection;
导入java.util.HashMap;
导入java.util.Map;
导入org.springframework.http.HttpStatus;
导入org.springframework.http.MediaType;
导入org.springframework.http.ResponseEntity;
导入org.springframework.web.bind.annotation.PathVariable;
导入org.springframework.web.bind.annotation.RequestBody;
导入org.springframework.web.bind.annotation.RequestMapping;
导入org.springframework.web.bind.annotation.RequestMethod;
导入org.springframework.web.bind.annotation.RestController;
导入com.example.model.Greeting;
@RestController
公共类迎宾控制器{
私有静态大整数nextId;
私有静态映射迎宾映射;
专用静态问候语保存(问候语){
if(greetingMap==null){
greetingMap=新HashMap();
nextId=biginger.ONE;
}
问候语.setId(nextId);
nextId=nextId.add(biginger.ONE);
greetingMap.put(greeting.getId(),greeting);
回敬问候;
}
静止的{
//第一句问候语
问候语g1=新问候语();
g1.setText(“你好,世界!!”);
保存(g1);
//第二次问候
问候语g2=新问候语();
g2.setText(“Hola Mundo!!”);
保存(g2);
}
/*  
* 
*发出“查看问候语”
* 
*/
@请求映射(
value=“/api/greetings”,
method=RequestMethod.GET,
products=MediaType.APPLICATION\u JSON\u值
)
公众反应{
集合问候语=greetingMap.values();
返回新的ResponseEntity(问候语,HttpStatus.OK);
}
/*  
* 
*发出“按id值查看单个问候语”命令
* 
*/
@请求映射(
value=“/api/greetings/{id}”,
method=RequestMethod.GET,
products=MediaType.APPLICATION\u JSON\u值
)
公共响应属性getGreeting(@PathVariable(“id”)BigInteger id){
Greeting Greeting=greetingMap.get(id);
if(问候语==null){
返回新的ResponseEntity(未找到HttpStatus.NOT_);
}
返回新的响应(问候语,HttpStatus.OK);
}
/*  
* 
*创建帖子以添加问候语
* 
*/
@请求映射(
value=“/api/greetings/”,
method=RequestMethod.POST,
consumes=MediaType.APPLICATION\u JSON\u值,
products=MediaType.APPLICATION\u JSON\u值
)
公共响应性createGreeting(@RequestBody Greeting){
问候语savedGreeting=保存(问候语);
返回新的ResponseEntity(savedGreeting,HttpStatus.CREATED);
}
/*结束HTTP方法*/
}


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


请尝试将该值链接到另一个值,例如:

@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);
    }
@RequestMapping(
value=“/api/greeting”,
method=RequestMethod.GET,
products=MediaType.APPLICATION\u JSON\u值
)
公共响应性createGreeting(@RequestBody Greeting){
问候语savedGreeting=保存(问候语);
返回新的ResponseEntity(savedGreeting,HttpStatus.CREATED);
}

您的POST方法有一个尾随斜杠
/api/greetings/
,您在curl调用中遗漏了该斜杠,另一个遗漏是
内容类型
标题。您应该告诉服务器您发送的数据类型


curl-xpost-d'{“text”:“hello”}'-H“内容类型:application/json”http://localhost:8080/api/greetings/
是一个有效的curl调用。

您需要将内容类型修改为json


您是如何执行post请求的?卷曲阿贾克斯电话?张贴代码。因为一切看起来都正常。我使用了curl和postman:curl-X POST'{“text”:“Bonjure Le Monde!!”}curl:(6)无法解析主机:“text”{“timestamp”:1447918719641,“status”:405,“error”:“Method not Allowed”,“exception”:“org.springframework.web.HttpRequestMethodNotSupportedException”,“message”:“请求方法”POST“not supported”,“path”:“/api/greetings”}尝试禁用csrf,这对我很有帮助。从您的意思是value=“/api/greetings”获取此解决方案" ? 你认为有什么价值。我试过使用/问候语,但也不起作用。我认为这两种方法都值得借鉴。制作不同的链接。我确实将值从/api/greetings更改为/api/greeting,我发布了如下帖子(结果相同):macs MacBook Pro:~mac$curl-X post'{“text”:“Bonjure Le Monde!!”}“curl:(6)无法解析主机:“text”{“timestamp”:1447924664834,“status”:405,“error”:“Method not Allowed”,“exception”::“org.springframework.web.HttpRequestMethodNotSupportedException”,“message:“请求方法'POST'不受支持”,“path:“/api/greeting”}非常感谢您修复了它,curl命令结尾缺少的斜杠。upvote和标记为已回答的表示感谢。感谢lotsuper,这正是我的问题,谢谢!