【问题标题】:Getting error with Spring MV 4.0 Restful webservice when I am trying RequestMethod.POST当我尝试 RequestMethod.POST 时,Spring MVC 4.0 Restful Web 服务出错
【发布时间】: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


    【解决方案1】:

    删除标题并尝试使用。

       @RequestMapping(value = "/countries1", method = RequestMethod.POST,consumes=MediaType.APPLICATION_JSON_VALUE)  
         public Desh addCountry(@RequestBody Desh country) {  
          return countryService.addCountry(country);  
         }  
    

    【讨论】:

      【解决方案2】:

      解决问题的正确方法是全局配置,因为您提到您正在开发基于 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>
      

      这将确保您的 JSON 请求对象在服务器-客户端通信期间转换为 tofrom

      然后,您可以从 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。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-06-13
        • 1970-01-01
        • 1970-01-01
        • 2014-04-20
        • 2015-04-13
        • 2015-02-20
        • 1970-01-01
        相关资源
        最近更新 更多