使用web服务Java时出现错误404-angular

使用web服务Java时出现错误404-angular,java,javascript,json,angularjs,Java,Javascript,Json,Angularjs,当我在firebug的控制器中使用JSON将对象从angular发送到java控制器时,会显示错误404。这意味着angular找不到此路径,但java后端会收到此消息。问题是因为我无法响应消息 这是json: {"title":"sdtgb","authors":[{"author_id":60,"author":"Brandon Sanderson"}],"genres":[{"genre_id":14,"genre":"Satyra"}],"description":"sdtb","pat

当我在firebug的控制器中使用JSON将对象从angular发送到java控制器时,会显示错误404。这意味着angular找不到此路径,但java后端会收到此消息。问题是因为我无法响应消息

这是json:

{"title":"sdtgb","authors":[{"author_id":60,"author":"Brandon Sanderson"}],"genres":[{"genre_id":14,"genre":"Satyra"}],"description":"sdtb","path_image":"19674.png"}
java控制器:

@SuppressWarnings("finally")
    @RequestMapping(value = "/rest/book", method = RequestMethod.POST)
    public MessageDTO addNewBook(@RequestBody BookDTO newBook) {
        MessageDTO message = new MessageDTO();
        try {
            bookService.addNewBook(newBook);
            message.setCheck(true);
        } catch (BookTitleException e) {
            message.setCheck(false);
            message.setDescription("Ksiązka o tym tytule juz istnieje.");
            e.printStackTrace();
        } finally{
            return message;
        }
    }
var book = {
            title : $scope.title,
            authors : $scope.author,
            genres : $scope.genre,
            description : $scope.description,
            path_image: null
        }

NewBookFct.addNewBook(book)
                .then( function(resolve){
                    if(resolve.check){
                        alert("Ksiazka została dodana.");
                    }else{
                        alert(resolve.description);
                    }
                }, function(reason){
                    console.log(reason);
                });
这是从firebug发送消息和错误时的路径:

网络错误:找不到404-http://localhost:8080/engineering-project web/rest/book

预订地址:

public class BookDTO implements Serializable{

    private static final long serialVersionUID = -5057364006691079475L;

    private AuthorEntity [] authors;    
    private String description; 
    private GenreEntity [] genres;  
    private String title;
    private String path_image;

    /* getters and setters */
}
Java脚本:

控制器:

@SuppressWarnings("finally")
    @RequestMapping(value = "/rest/book", method = RequestMethod.POST)
    public MessageDTO addNewBook(@RequestBody BookDTO newBook) {
        MessageDTO message = new MessageDTO();
        try {
            bookService.addNewBook(newBook);
            message.setCheck(true);
        } catch (BookTitleException e) {
            message.setCheck(false);
            message.setDescription("Ksiązka o tym tytule juz istnieje.");
            e.printStackTrace();
        } finally{
            return message;
        }
    }
var book = {
            title : $scope.title,
            authors : $scope.author,
            genres : $scope.genre,
            description : $scope.description,
            path_image: null
        }

NewBookFct.addNewBook(book)
                .then( function(resolve){
                    if(resolve.check){
                        alert("Ksiazka została dodana.");
                    }else{
                        alert(resolve.description);
                    }
                }, function(reason){
                    console.log(reason);
                });
服务:

service.addNewBook = function(book){
        var deferred = $q.defer();
        bookResource.save(book)
            .$promise.then( function(data){
                deferred.resolve( data );
            }, function(){
                deferred.reject("Error during adding new book.");
            });
        return deferred.promise;
    }

很可能您是从localhost:3000或其他端口运行Angular应用程序,从localhost:8080运行Java Web。这意味着您需要发出跨站点请求,这通常是出于安全目的而阻止的

您需要在web应用程序上配置CORS。这可以通过添加库和配置web.xml轻松完成。请在此处阅读更多信息:

如果您使用的是maven,只需添加以下依赖项:

<dependency>
    <groupId>com.thetransactioncompany</groupId>
    <artifactId>cors-filter</artifactId>
    <version>2.5</version>
</dependency>
然后在web.xml中添加如下内容:

<filter>
    <filter-name>CORS</filter-name>
    <filter-class>com.thetransactioncompany.cors.CORSFilter</filter-class>
    <init-param>
        <param-name>cors.supportedMethods</param-name>
        <param-value>GET, POST, HEAD, PUT, DELETE, OPTIONS</param-value>
    </init-param>
</filter>
<filter-mapping>
    <filter-name>CORS</filter-name>
    <url-pattern>/api/*</url-pattern>
</filter-mapping>

在上面的配置中,我使CORS能够在我的web应用程序的/api路径下获取、发布、发送、放置、删除和选项请求到任何URL。

你能显示你的js代码吗?我发现Chrome+非常适合调试HTTP。是的,我能。我把JS代码。