无法将JSON数据发布到Jersey Restful Web服务

时间:2016-02-19 05:11:16

标签: java rest jersey

我正在尝试将JSON数据发送到我的RESTful Web服务。对于简单的GET类型,配置工作正常。只有问题是POST类型。

我的角色代码

var app = angular.module('myApp', []);
app.controller('MyController', function($scope,$http) {
$scope.pushDataToServer = function() {
    $http({
        method: 'POST',         
        url: 'rest/Board/autoSave',
        headers: {'Content-Type': 'application/json'},
        data:'{"firstName":"Syed", "lastName":"Chennai"}',
        }).success(function (data){
            $scope.status=data;
        }).error(function(data, status, headers, config) {
            alert("error");
       });      
    };
});

我的Java代码

@Path("/Board")
public class BoardAutoSaveService {

    @POST
    @Path("/autoSave")
    @Consumes({MediaType.APPLICATION_JSON})
    @Produces({MediaType.TEXT_PLAIN})
    public String  autoSave(Message msg) throws Exception{
        System.out.println("Calling Autosave.");
        System.out.println("First Name = "+msg.getFirstName());
        System.out.println("Last Name  = "+msg.getLastName());
        return null;        
    }
}

消息类

package com.intu;
import java.util.Date;
public class Message {

private String firstName;

private String lastName;

private int age;

private Date date;

private String text;



public String getFirstName() {
    return firstName;
}

public void setFirstName(String firstName) {
    this.firstName = firstName;
}

public String getLastName() {
    return lastName;
}

public void setLastName(String lastName) {
    this.lastName = lastName;
}

public int getAge() {
    return age;
}

public void setAge(int age) {
    this.age = age;
}

public Date getDate() {
    return date;
}

public void setDate(Date date) {
    this.date = date;
}

public String getText() {
    return text;
}

public void setText(String text) {
    this.text = text;
}

}

Web.xml中

      <?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
  <display-name>iNTU-1</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>   
  </welcome-file-list>
  <servlet>
      <servlet-name>Jersey RESTful Application</servlet-name>
      <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
       <init-param>
        <param-name>jersey.config.server.provider.packages</param-name>
        <param-value>com.dao</param-value>
       </init-param>
     </servlet>
   <servlet-mapping>
      <servlet-name>Jersey RESTful Application</servlet-name>
      <url-pattern>/rest/*</url-pattern>
    </servlet-mapping>
   </web-app>

我错过了什么吗?

错误是

SEVERE: A message body reader for Java class com.intu.Message, and Java type class com.intu.Message, and MIME media type application/json was not found.
The registered message body readers compatible with the MIME media type are:
application/json ->
  com.sun.jersey.json.impl.provider.entity.JSONJAXBElementProvider$App
  com.sun.jersey.json.impl.provider.entity.JSONRootElementProvider$App
  com.sun.jersey.json.impl.provider.entity.JSONListElementProvider$App
*/* ->

1 个答案:

答案 0 :(得分:0)

您只有默认的Jersey JSON提供程序,在您的模型类上需要@XmlRootElement。您可以看到可用的提供商,其中一个是JSON**RootElement**Provider

com.sun.jersey.json.impl.provider.entity.JSONJAXBElementProvider$App
com.sun.jersey.json.impl.provider.entity.JSONRootElementProvider$App
com.sun.jersey.json.impl.provider.entity.JSONListElementProvider$App

此提供程序用于(de)序列化使用@XmlRootElement注释的类。

如果您有以下依赖

<dependency>
    <groupId>com.sun.jersey</groupId>
    <artifactId>jersey-json</artifactId>
    <version>${jersey.version}</version>
</dependency>
然后它还应该提供杰克逊提供商,它不需要@XmlRootElement,但你仍然需要配置泽西岛使用杰克逊而不是默认提供商。要配置它,您需要将其添加到web.xml Jersey servlet配置中,如here

所示
<init-param>
    <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
    <param-value>true</param-value>
</init-param>

或者如果您没有使用web.xml,请在ResourceConfig子类构造函数中添加以下内容

public class AppConfig extends PackagesResourceConfig{

    public AppConfig() {
        getProperties().put(JSONConfiguration.FEATURE_POJO_MAPPING, true);
    }
}