我正在使用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,request服务器拒绝了此请求,因为请求实体所采用的方法所请求的资源不支持该格式()。
在Postman中我设置了头部接受和内容类型“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;
}
}
答案 0 :(得分:0)
删除标题并尝试使用。
@RequestMapping(value = "/countries1", method = RequestMethod.POST,consumes=MediaType.APPLICATION_JSON_VALUE)
public Desh addCountry(@RequestBody Desh country) {
return countryService.addCountry(country);
}
答案 1 :(得分:0)
正如您提到的,您正在开发基于json
的应用程序,正确的方法来解决您的问题。
请在配置xml文件中添加以下内容,
<!-- Configure to plugin JSON as request and response in method handler -->
<beans:bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<beans:property name="messageConverters">
<beans:list>
<beans:ref bean="jsonMessageConverter"/>
</beans:list>
</beans:property>
</beans:bean>
<!-- Configure bean to convert JSON to POJO and vice versa -->
<beans:bean id="jsonMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
</beans:bean>
这将确保在服务器 - 客户端通信期间转换{J}请求对象to
和from
。
然后,您可以从controller
中的处理程序方法中删除标题。
@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.POST)
public Desh addCountry(@RequestBody Desh country) {
return countryService.addCountry(country);
}
另外,请确保您已在依赖项中正确添加jackson
个jar。