AngularJS REST示例DELETE方法错误请求400

时间:2013-09-17 06:10:19

标签: angularjs jersey tomcat7 http-status-code-400 http-delete

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

到目前为止,我设法将GET和POST METHOD实现为REST服务,并且它们可以工作。 实际上我正试图实现DELETE方法,但我继续在浏览器中检索HTTP Error 400 Bad Request。 二手浏览器是在ubuntu OS上运行的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方法时遇到错误请求? 任何想法提示都表示赞赏。

1 个答案:

答案 0 :(得分:1)

这里似乎有几个问题:

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

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

希望这有帮助