1

I'm trying to send JSON data to my RESTful web service. Configuration is working fine for a simple GET type. Only the issue is for POST type.

My Angular code

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");
       });      
    };
});

My Java code

@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;        
    }
}

Message class

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>

Am I missing anything?

The error is

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
*/* ->
Sindhoo Oad
  • 1,194
  • 2
  • 13
  • 29
Syed
  • 2,471
  • 10
  • 49
  • 89

1 Answers1

0

You only have the default Jersey JSON providers, which require @XmlRootElement on your model class. You can see the available providers, one of which is the 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

This provider is used to (de)serialize classes annotated with @XmlRootElement.

If you have the following dependency

<dependency>
    <groupId>com.sun.jersey</groupId>
    <artifactId>jersey-json</artifactId>
    <version>${jersey.version}</version>
</dependency>

then it should also pull in the Jackson provider, which doesn't require @XmlRootElement, but you still need to configure Jersey to use Jackson instead of its default provider. To configure it you need to add this to your web.xml Jersey servlet configuration, as seen here

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

Or if you are not using a web.xml, add the following inside your ResourceConfig subclass constructor

public class AppConfig extends PackagesResourceConfig{

    public AppConfig() {
        getProperties().put(JSONConfiguration.FEATURE_POJO_MAPPING, true);
    }
}
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720