2014-11-04 74 views
0

我在春天定义的REST服务如下:映射JSON对象春对象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)。有任何想法吗?

+0

你尝试过'Content-Type':'application/json'吗? – luboskrnac 2014-11-04 18:35:05

回答

1

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

REST处理

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

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

首先

您有:

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

只是/create

您有:

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

继续删除.json这就是问题所在

一定要在对于ajax事件,你发送的数据是在JSON