未找到Java类型和MIME媒体类型的消息正文阅读器application / json; charset = utf-8

时间:2016-03-02 05:27:24

标签: java angularjs jersey

我正在使用一个将AngularJS作为客户端和Jersey Rest WebService的应用程序。我可以使用angular $ http.get()成功获取数据,但我很难将数据发布到服务器。我收到以下异常。

com.sun.jersey.spi.container.ContainerRequest getEntity 严重:未找到Java类型的消息正文阅读器,类dto.BookObject和MIME媒体类型application / json; charset = utf-8

BookObject.java

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

}

在fiddler响应中,我可以看到以下错误消息: HTTP错误415不支持的媒体类型

请建议我摆脱上面提到的错误。

2 个答案:

答案 0 :(得分:0)

您在POJO中缺少注释@XmlRootElement,因此球衣无法弄清楚如何解组JSON。

答案 1 :(得分:0)

我已经注释了我的POJO(@xmlRootElement),我没有在我的项目中使用Maven。这个问题通过放置新泽西图书馆解决,Ealrier我使用的是jersey-1.3.jar,但我使用的是1.7版的球衣。