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
*/* ->