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

我正在盯着弹簧靴并尝试一个简单的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 greetingMap;

private static Greeting save(Greeting greeting){

if (greetingMap==null){

greetingMap = new HashMap();

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> getGreetings(){

Collection greetings=greetingMap.values();

return new ResponseEntity>(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 getGreeting(@PathVariable("id") BigInteger id){

Greeting greeting = greetingMap.get(id);

if(greeting == null){

return new ResponseEntity(HttpStatus.NOT_FOUND);

}

return new ResponseEntity (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 createGreeting(@RequestBody Greeting greeting){

Greeting savedGreeting = save(greeting);

return new ResponseEntity (savedGreeting, HttpStatus.CREATED);

}

/* End of HTTP Methods */

}

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

亲切的问候

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值