1

我是 AngularJS 的新手,我正在尝试实现一个 CRUD REST Angular 应用程序。作为 REST 服务的服务器,我使用的是带有 Jersey 的 tomcat 7 服务器。

到目前为止,我设法将 GET 和 POST 方法实现为 REST 服务,并且它们可以工作。实际上我正在尝试实现 DELETE 方法,但我继续在浏览器中检索 HTTP 错误 400 错误请求。使用的浏览器是在 ubuntu 操作系统上运行的 Chrome 29。

我的 REST 服务

import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Request;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;
import javax.xml.bind.JAXBElement;

@Path("/recipe")
public class RecipeResource {

    @Context
    private UriInfo uriInfo;
    @Context
    private Request request;

    private String id;

    @GET
    @Produces({ MediaType.APPLICATION_JSON })
    public List<Recipe> getRecipes() {
        List<Recipe> dummyData = new ArrayList<>();
        dummyData.add(new Recipe(new Long(1), "Recipe1", "Description1", null));
        dummyData.add(new Recipe(new Long(2), "Recipe2", "Description2", null));
        dummyData.add(new Recipe(new Long(3), "Recipe3", "Description3", null));
        dummyData.add(new Recipe(new Long(4), "Recipe4", "Description4", null));
        dummyData.add(new Recipe(new Long(5), "Recipe5", "Description5", null));
        dummyData.add(new Recipe(new Long(6), "Recipe6", "Description6", null));
        dummyData.add(new Recipe(new Long(7), "Recipe7", "Description7", null));
        dummyData.add(new Recipe(new Long(8), "Recipe8", "Description8", null));
        dummyData.add(new Recipe(new Long(9), "Recipe9", "Description9", null));
        System.out
                .println("REST Service Method getRecipes called by JSON Client");

        System.out.println("Called URI: " + uriInfo.getAbsolutePath());
        return dummyData;
    }

    @GET
    @Produces({ MediaType.TEXT_XML, MediaType.APPLICATION_XML })
    public List<Recipe> getRecipesBrowser() {
        System.out.println("REST Service Method getRecipesBrowser called");
        System.out.println("Called URI: " + uriInfo.getAbsolutePath());
        List<Recipe> dummyData = new ArrayList<>();
        dummyData.add(new Recipe(new Long(1), "Recipe1", "Description1", null));
        dummyData.add(new Recipe(new Long(2), "Recipe2", "Description2", null));
        dummyData.add(new Recipe(new Long(3), "Recipe3", "Description3", null));
        dummyData.add(new Recipe(new Long(4), "Recipe4", "Description4", null));
        dummyData.add(new Recipe(new Long(5), "Recipe5", "Description5", null));
        dummyData.add(new Recipe(new Long(6), "Recipe6", "Description6", null));
        dummyData.add(new Recipe(new Long(7), "Recipe7", "Description7", null));
        dummyData.add(new Recipe(new Long(8), "Recipe8", "Description8", null));
        dummyData.add(new Recipe(new Long(9), "Recipe9", "Description9", null));

        return dummyData;
    }

    @GET
    @Path("/{id}")
    @Produces({ MediaType.APPLICATION_JSON, MediaType.TEXT_XML,
            MediaType.APPLICATION_XML })
    public Recipe getRecipeById(@PathParam("id") int id) {
        System.out.println("REST Service Method getRecipeById called");
        System.out.println("Called URI: " + uriInfo.getAbsolutePath());
        // if (id.equals("1")) {
        // return new Recipe(new Long(3), "Recipe3", "Description3", null);
        // }
        if (id <= 10) {
            return new Recipe(new Long(id), "Recipe" + id, "Description" + id,
                    null);
        }

        return null;
    }

    @PUT
    @Path("/{id}")
    @Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
    public Response updateRecipe(JAXBElement<Recipe> recipe) {
        System.out.println("REST Service Method updateRecipe called");
        System.out.println("Called URI: " + uriInfo.getAbsolutePath());
        Recipe newRecipe = recipe.getValue();
        return Response.created(uriInfo.getAbsolutePath()).build();
    }

    @DELETE
    @Path("/{id}")
    @Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
    public Response deleteRecipe(JAXBElement<Recipe> recipe) {
        System.out.println("REST Service Method deleteRecipe called");
        System.out.println("Called URI: " + uriInfo.getAbsolutePath());
        Recipe newRecipe = recipe.getValue();
        return Response.created(uriInfo.getAbsolutePath()).build();
    }

}

我的 webapp 的 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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    id="WebApp_ID" version="2.5">
    <display-name>com.example.webservice</display-name>
    <servlet>
        <servlet-name>Jersey REST Service</servlet-name>
        <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
        <init-param>
            <param-name>com.sun.jersey.config.property.packages</param-name>
            <param-value>com.xxxx.yyyyy.services</param-value>
        </init-param>
        <init-param>
            <param-name>readonly</param-name>
            <param-value>false</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Jersey REST Service</servlet-name>
        <url-pattern>/rest/*</url-pattern>
    </servlet-mapping>
    <filter>
        <filter-name>CorsFilter</filter-name>
        <filter-class>org.apache.catalina.filters.CorsFilter</filter-class>
        <init-param>
            <param-name>cors.allowed.origins</param-name>
            <param-value>*</param-value>
        </init-param>
        <init-param>
            <param-name>cors.allowed.methods</param-name>
            <param-value>GET,POST,HEAD,OPTIONS,PUT,DELETE</param-value>
        </init-param>
        <init-param>
            <param-name>cors.allowed.headers</param-name>
            <param-value>Content-Type,X-Requested-With,accept,Origin,Access-Control-Request-Method,Access-Control-Request-Headers</param-value>
        </init-param>
        <init-param>
            <param-name>cors.exposed.headers</param-name>
            <param-value>Access-Control-Allow-Origin,Access-Control-Allow-Credentials</param-value>
        </init-param>
        <init-param>
            <param-name>cors.support.credentials</param-name>
            <param-value>true</param-value>
        </init-param>
        <init-param>
            <param-name>cors.preflight.maxage</param-name>
            <param-value>10</param-value>
        </init-param>

    </filter>
    <filter-mapping>
        <filter-name>CorsFilter</filter-name>
        <url-pattern>/*</url-pattern>

    </filter-mapping>
</web-app>

这是我的 AngularJS 项目的资源定义:

angular.module('myApp.recipeService', ['ngResource']).
factory('RecipeService', function($resource){
    return $resource('http://localhost\\:10080/CookstarServices/rest/recipe/:recipeId', {}, {
        getRecipes: {method:'GET', params:{recipeId: ''}, isArray:false},
        post: {method:'POST'},
        update: {method:'PUT', params: {recipeId: '@recipeId'}},
        remove: {method:'DELETE'}
    });
});

有没有人告诉我为什么在尝试调用 DELETE 方法时收到错误请求?任何想法提示表示赞赏。

4

1 回答 1

1

这里似乎有几个问题:

1-看起来您在 DELETE 端点中期待一个 recipeId。但是在您的资源定义中,remove 方法没有设置 recipeId 参数。

2- 您的 deleteRecipe 方法是您的 updateRecipe 方法的副本,并且期望提交完整的 Recipe 对象而不仅仅是 recipeId

希望这可以帮助

于 2013-09-17T07:46:48.603 回答