2014-09-24 48 views
0

我是Angular JS和REST服務的新手,剛剛嘗試hello world。AngularJS POST請求映射到REST服務中的DTO

的web.xml

<servlet> 
     <servlet-name>jersey-helloworld-serlvet</servlet-name> 
     <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class> 
     <init-param> 
      <param-name>com.sun.jersey.config.property.packages</param-name> 
      <param-value>com.ipocc.service</param-value> 
     </init-param> 
     <init-param> 
      <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name> 
      <param-value>true</param-value> 
     </init-param> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 

REST服務類

​​

DTO

@XmlRootElement 
public class User { 
    @XmlElement public String email; 
    @XmlElement public String name; 
    @XmlElement public String username; 
    //GETTER-SETTER 
} 

ANGULAR JS文件

var validationApp = angular.module('validationApp', []); 
validationApp.controller('mainController', function($scope,$http) { 
    $scope.submitForm = function() { 

      var update_path = "http://localhost:8080/IPOCCService/rest/UserManager/validate"; 
      var data1 = angular.toJson($scope.user); 
      alert(data1);    
      $http({ 
        url: update_path, 
        method: "POST", 
        data: data1, 
        headers : { 
         "Content-Type" : "application/json; charset=utf-8", 
         "Accept" : "application/json" 
        } 
      }). 
      success(function(data, status, headers, config) { 
       alert("success"); 
      }). 
      error(function(data, status, headers, config) { 
       alert("failure"); 
      }); 

    }; 
}); 

的錯誤,我現在面臨

SEVERE: A message body reader for Java class com.ipocc.service.dto.User, and Java type class com.ipocc.service.dto.User, and MIME media type application/json; charset=UTF-8 was not found. 
The registered message body readers compatible with the MIME media type are: 
*/* -> 
    com.sun.jersey.core.impl.provider.entity.FormProvider 
    com.sun.jersey.core.impl.provider.entity.StringProvider 
    com.sun.jersey.core.impl.provider.entity.ByteArrayProvider 
    com.sun.jersey.core.impl.provider.entity.FileProvider 
    com.sun.jersey.core.impl.provider.entity.InputStreamProvider 
    com.sun.jersey.core.impl.provider.entity.DataSourceProvider 
    com.sun.jersey.core.impl.provider.entity.XMLJAXBElementProvider$General 
    com.sun.jersey.core.impl.provider.entity.ReaderProvider 
    com.sun.jersey.core.impl.provider.entity.DocumentProvider 
    com.sun.jersey.core.impl.provider.entity.SourceProvider$StreamSourceReader 
    com.sun.jersey.core.impl.provider.entity.SourceProvider$SAXSourceReader 
    com.sun.jersey.core.impl.provider.entity.SourceProvider$DOMSourceReader 
    com.sun.jersey.core.impl.provider.entity.XMLRootElementProvider$General 
    com.sun.jersey.core.impl.provider.entity.XMLListElementProvider$General 
    com.sun.jersey.core.impl.provider.entity.XMLRootObjectProvider$General 
    com.sun.jersey.core.impl.provider.entity.EntityHolderReader 

我想實現當我打電話從角JS HTTP POST請求,它應該得到映射到用戶對象,但我上面得到錯誤。如果我保持參數爲字符串,那麼我得到的數據爲email:"[email protected]",name: "j",username: "j"

回答

0

我不熟悉澤西島,但它看起來像缺少jersey-json依賴關係來解析接收到的JSON數據。 - SO Link

+0

我有jersey-json依賴項 – Jayesh 2014-09-24 12:14:33

0

,如果它可以幫助別人......

我作出錯誤的錯誤道歉。在我的依賴項中,jar版本不匹配,現在它在pom.xml中進行如下更改後工作:

<dependencies> 

     <dependency> 
      <groupId>com.sun.jersey</groupId> 
      <artifactId>jersey-server</artifactId> 
      <version>1.17</version> 
     </dependency> 
     <dependency> 
      <groupId>com.sun.jersey</groupId> 
      <artifactId>jersey-json</artifactId> 
      <version>1.17</version> 
     </dependency> 
     <dependency> 
      <groupId>com.sun.jersey</groupId> 
      <artifactId>jersey-client</artifactId> 
      <version>1.17</version> 
     </dependency> 
     <dependency> 
      <groupId>com.sun.jersey</groupId> 
      <artifactId>jersey-servlet</artifactId> 
      <version>1.17</version> 
     </dependency> 

    </dependencies>