Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
AngularJS REST示例删除方法错误请求400_Angularjs_Jersey_Tomcat7_Http Status Code 400_Http Delete - Fatal编程技术网

AngularJS REST示例删除方法错误请求400

AngularJS REST示例删除方法错误请求400,angularjs,jersey,tomcat7,http-status-code-400,http-delete,Angularjs,Jersey,Tomcat7,Http Status Code 400,Http Delete,我是AngularJS的新手,我正在尝试实现一个CRUD REST Angular应用程序。 作为REST服务的服务器,我使用的是带有Jersey的Tomcat7服务器 到目前为止,我设法将GET和POST方法实现为REST服务,并且它们可以工作。 实际上,我正在尝试实现DELETE方法,但我继续在浏览器中检索HTTP错误400错误请求。 使用的浏览器是运行在ubuntu操作系统上的Chrome 29 我的休息服务 import javax.ws.rs.Consumes; import java

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

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

我的休息服务

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

}
有人告诉我为什么我在尝试调用DELETE方法时收到错误的请求吗?
任何想法和提示都非常感谢。

这里似乎有几个问题:

1-看起来您希望删除端点中出现recipeId。但在资源定义中,remove方法不设置recipeId参数

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


希望这有帮助

这里似乎有几个问题:

1-看起来您希望删除端点中出现recipeId。但在资源定义中,remove方法不设置recipeId参数

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


希望这有帮助

感谢您的回复。我从服务中删除了JAxBElement定义,现在控制台日志没有报告任何错误。现在将尝试添加Id作为参数。很好,很高兴它对您有效,也许可以将此标记为正确答案?:)谢谢你的回复。我从服务中删除了JAxBElement定义,现在控制台日志没有报告任何错误。现在将尝试添加Id作为参数。很好,很高兴它对您有效,也许可以将此标记为正确答案?:)
<?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>
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'}
    });
});