【发布时间】:2017-05-08 09:25:59
【问题描述】:
我正在使用 Postman 控制台访问服务(http://localhost:8080/MyResful/countries1),它使用 GET 方法工作正常并给出以下响应
[
{
"id": 1,
"countryName": "India",
"population": 10000
},
{
"id": 2,
"countryName": "Pakistan",
"population": 7000
},
{
"id": 3,
"countryName": "Nepal",
"population": 8000
},
{
"id": 4,
"countryName": "China",
"population": 20000
}
]
但它不适用于 (http://localhost:8080/MyResful/countries1)POST 方法并给出错误:
HTTP 状态 415,请求服务器拒绝此请求,因为请求实体的格式不受所请求方法 () 的请求资源支持。
在 Postman 中,我设置了标头 Accept 和 Content-type "application/JSON"
请帮助我解决这个问题。
我正在使用 JSON 对象处理 Spring MVC
这是我的控制器类:
package com.ness.myrestful.controller;
import java.util.List;
import com.ness.myrestful.bean.Desh;
import com.ness.myrestful.service.DeshService;
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;
@RestController
public class CrudRestController {
DeshService countryService = new DeshService();
@RequestMapping(value = "/countries1", method = RequestMethod.GET, headers = "Accept=application/json")
public List<Desh> getCountries() {
List<Desh> listOfCountries = countryService.getAllCountries();
return listOfCountries;
}
@RequestMapping(value = "/country1/{id}", method = RequestMethod.GET, headers = "Accept=application/json")
public Desh getCountryById(@PathVariable int id) {
return countryService.getCountry(id);
}
@RequestMapping(value = "/countries1", method = RequestMethod.POST, headers = "Accept=application/json")
public Desh addCountry(@RequestBody Desh country) {
return countryService.addCountry(country);
}
@RequestMapping(value = "/countries1", method = RequestMethod.PUT, headers = "Accept=application/json")
public Desh updateCountry(@RequestBody Desh country) {
return countryService.updateCountry(country);
}
@RequestMapping(value = "/country1/{id}", method = RequestMethod.DELETE, headers = "Accept=application/json")
public void deleteCountry(@PathVariable("id") int id) {
countryService.deleteCountry(id);
}
}
********************
我正在使用 JSON 对象处理 Spring MVC
这是 Bean 类
package com.ness.myrestful.bean;
public class Desh {
int id;
String countryName;
long population;
public Desh() {
super();
}
public Desh(int i, String countryName,long population) {
super();
this.id = i;
this.countryName = countryName;
this.population=population;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getCountryName() {
return countryName;
}
public void setCountryName(String countryName) {
this.countryName = countryName;
}
public long getPopulation() {
return population;
}
public void setPopulation(long population) {
this.population = population;
}
}
********************************
我正在使用 JSON 对象处理 Spring MVC 这是我的服务类
package com.ness.myrestful.service;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import com.ness.myrestful.bean.Desh;
public class DeshService {
static HashMap<Integer,Desh> countryIdMap=getCountryIdMap();
public DeshService() {
super();
if(countryIdMap==null)
{
countryIdMap=new HashMap<Integer,Desh>();
// Creating some objects of Country while initializing
Desh indiaCountry=new Desh(1, "India",10000);
Desh chinaCountry=new Desh(4, "China",20000);
Desh nepalCountry=new Desh(3, "Nepal",8000);
Desh bhutanCountry=new Desh(2, "Pakistan",7000);
countryIdMap.put(1,indiaCountry);
countryIdMap.put(4,chinaCountry);
countryIdMap.put(3,nepalCountry);
countryIdMap.put(2,bhutanCountry);
}
}
public List<Desh> getAllCountries()
{
List<Desh> countries = new ArrayList<Desh>(countryIdMap.values());
return countries;
}
public Desh getCountry(int id)
{
Desh country= countryIdMap.get(id);
return country;
}
public Desh addCountry(Desh country)
{
country.setId(getMaxId()+1);
countryIdMap.put(country.getId(), country);
return country;
}
public Desh updateCountry(Desh country)
{
if(country.getId()<=0)
return null;
countryIdMap.put(country.getId(), country);
return country;
}
public void deleteCountry(int id)
{
countryIdMap.remove(id);
}
public static HashMap<Integer, Desh> getCountryIdMap() {
return countryIdMap;
}
// Utility method to get max id
public static int getMaxId()
{ int max=0;
for (int id:countryIdMap.keySet()) {
if(max<=id)
max=id;
}
return max;
}
}
【问题讨论】:
标签: json web-services rest spring-mvc