I am working with an application which has AngularJS as a client and Jersey Rest WebService. I am able to get the data successfully by using angular $http.get(), but i am struggling to post the data to the server. I am getting the following exception.
com.sun.jersey.spi.container.ContainerRequest getEntity SEVERE: A message body reader for Java type, class dto.BookObject, and MIME media type, application/json;charset=utf-8, was not found
package dto;
public class BookObject {
private long id;
private String author;
private String name;
private String series;
private int pages;
private boolean availability;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSeries() {
return series;
}
public void setSeries(String series) {
this.series = series;
}
public int getPages() {
return pages;
}
public void setPages(int pages) {
this.pages = pages;
}
public boolean isAvailability() {
return availability;
}
public void setAvailability(boolean availability) {
this.availability = availability;
}
}
var bookApp = angular.module('sharpLibraryApp', []);
bookApp.controller('bookAppCtrl', function($scope, $http) {
$http.get("http://localhost:8080/RESTfulProject/REST/WebService/GetBooks").
then(function(response) {
$scope.books = response.data;
},
function getError(response) {
});
$scope.addBook = function() {
alert('Adding book...');
$scope.books.push({"id": $scope.id, "name": $scope.name, "author": $scope.author, "pages": $scope.pages, "availability": $scope.availability });
alert('books data:');
var bookObj = {'id' : $scope.id, 'name' : $scope.name, 'author' : $scope.author, 'pages' : $scope.pages, 'availability' : $scope.availability
};
$http({
method : "POST",
url : "http://localhost:8080/RESTfulProject/REST/WebService/storeBook",
dataType: 'json',
data: '',
headers: {
"Content-Type": "application/json"
}
}, bookObj).then(function(response) {
alert('success');
$scope.message = response.data;
}, function(response) {
alert('failure');
$scope.message = response.status;
});
/* $http.post('http://localhost:8080/RESTfulProject/REST/WebService/storeBook', bookObj).
then(function(response) {
alert('success');
$scope.message = response.data.statusText;
},
function myError(response) {
alert('failed' + response.status);
$scope.message = response.data.statusText;
});
*/ $scope.id = "";
$scope.name = "";
$scope.author = "";
$scope.pages = "";
$scope.availability = false;
};
$scope.resetBook = function() {
$scope.id = "";
$scope.name = "";
$scope.author = "";
$scope.pages = "";
$scope.availability = false;
};
});
bookApp.controller('optionCtrl', function($scope, $http) {
$scope.options = ["true", "false"];
});
package webService;
import java.util.List;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import model.LibraryManager;
import com.google.gson.Gson;
import dto.BookObject;
@Path("/WebService")
public class BookService {
LibraryManager libraryMgr = new LibraryManager();
Gson gson = new Gson();
@GET
@Path("/GetBooks")
@Produces("application/json")
public String book()
{
String books = null;
try
{
List<BookObject> bookData = libraryMgr.GetBooks();
System.out.println(gson.toJson(bookData));
books = gson.toJson(bookData);
} catch (Exception e)
{
System.out.println("error");
}
return books;
}
@POST
@Path("/storeBook")
@Consumes({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML, "application/x-www-form-urlencoded"})
public Response putBooks(BookObject bookObj) {
try {
libraryMgr.storeBooks(bookObj);
} catch (Exception e) {
e.printStackTrace();
}
return Response.ok().build();
}
}
In fiddler response I can see the below error message: HTTP Error 415 Unsupported media type
Please suggest me to get rid of the above mentioned error.
您在POJO中缺少注解@XmlRootElement
,因此jersey无法确定如何解组JSON。
I have annotated my POJO(@xmlRootElement) and I am not using Maven in my project. This problem is resolved by placing the new Jersey Libraries, Ealrier I was using jersey-1.3.jar but I am using 1.7 version of jersey.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.