【问题标题】:Mapping json object to Spring object in REST service将 json 对象映射到 REST 服务中的 Spring 对象
【发布时间】:2014-12-31 18:20:05
【问题描述】:

我在 Spring 中定义了一个 REST 服务,如下所示:

  @RequestMapping(value = "/create", method = RequestMethod.POST)
   @ResponseBody
   public ResponseEntity<String> addArticle(@RequestBody Article article){
      try{
         articleService.addArticle(article.getTitle(),
                                   article.getContent(),
                                   article.getTags(),
                                   article.getPublishStatus(),
                                   article.getCompanyId(),
                                   article.getCategory());
         return new ResponseEntity<String>(HttpStatus.OK);
      } catch (Exception e){
         e.printStackTrace();
         return new ResponseEntity<String>(e.getMessage(), HttpStatus.OK);
      }
   }

而我的文章定义如下:

public class Article {

   private int id;

   private String title;

   private String content;

   private String smsContent;

   public String getSmsContent()
{
    return smsContent;
}



public void setSmsContent(String smsContent)
{
    this.smsContent = smsContent;
}


private String[] tags;

   private int companyId;

   private String category;


   public String getCategory(){
      return category;
   }



   public void setCategory(String category){
      this.category = category;
   }


   private byte publishStatus;


   public String getTitle(){
      return title;
   }


   public void setTitle(String title){
      this.title = title;
   }


   public String getContent(){
      return content;
   }


   public void setContent(String content){
      this.content = content;
   }


   public String[] getTags(){
      return tags;
   }


   public void setTags(String[] tags){
      this.tags = tags;
   }


   public int getCompanyId(){
      return companyId;
   }


   public void setCompanyId(int companyId){
      this.companyId = companyId;
   }


   public byte getPublishStatus(){
      return publishStatus;
   }


   public void setPublishStatus(byte publishStatus){
      this.publishStatus = publishStatus;
   }



   public int getId(){
      return id;
   }



   public void setId(int id){
      this.id = id;
   }



}

如何使用 Angular 调用此服务?我尝试了以下代码:

function createArticle(name, companyId, title, content, tags, category) {
                var request = $http({
                    method : 'POST',
                    url : '/inbound/api/article/create.json',
                    headers : {
                        'Content-Type' : 'application/x-www-form-urlencoded'
                    },
                    transformRequest : function(obj) {
                        var str = [];
                        for ( var p in obj)
                            str.push(encodeURIComponent(p) + "="
                                    + encodeURIComponent(obj[p]));
                        return str.join("&");
                    },
                    data : {
                        title : title,
                        content : content,
                        tags : tags,
                        companyId : companyId,
                        category: category
                    }
                });

我收到错误415 (Unsupported Media Type)。有什么想法吗?

【问题讨论】:

  • 你试过 'Content-Type' : 'application/json' 吗?

标签: json angularjs spring rest spring-mvc


【解决方案1】:

由于您使用的是 JSON,因此您需要相应地设置表单和处理程序。

REST 处理程序

@RequestMapping(value = "/create", method = RequestMethod.POST, consumes = "application/json")

角度

headers : {
    'Content-Type' : 'application/json'
},

【讨论】:

    【解决方案2】:

    第一

    你有:

    @RequestMapping(value = "/create", method = RequestMethod.POST)
    

    只需/create

    第二

    你有:

    url : '/inbound/api/article/create.json',
    

    继续删除问题所在的.json

    第三

    一定要为ajax事件指明,你发送的数据是JSON格式

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-11-27
      • 2019-05-27
      • 2014-07-26
      • 2014-11-07
      • 2012-04-23
      • 2016-12-18
      • 2012-02-06
      相关资源
      最近更新 更多