gpt4 book ai didi

java - 删除带有有效负载或表单数据的请求会导致错误请求

转载 作者:行者123 更新时间:2023-12-02 15:27:25 24 4
gpt4 key购买 nike

我正在使用 Java Jersey 2.17 构建 RESTful Web 服务。它的客户。我正在使用 ExtJS 5 进行开发。

我的服务类(class)

主.java

 public class Main
{
public static final String BASE_URI = "http://localhost:8080/app/rest/";
public static HttpServer startServer() {
final ResourceConfig rc = new ResourceConfig();

rc.packages(true, "app");
rc.register(ResponseCorsFilter.class);

return GrizzlyHttpServerFactory.createHttpServer(URI.create(Main.BASE_URI), rc);
}

public static void main(final String[] args) throws IOException {
final HttpServer server = Main.startServer();
System.out.println(String.format("Jersey app started with WADL available at " + "%sapplication.wadl\nHit enter to stop it...",
Main.BASE_URI));
System.in.read();
server.stop();
}
}

UserRi.java

@Path("/user")
public class UsersRi {
@DELETE
@Path("/{id}")
@Produces(MediaType.APPLICATION_JSON)
public String deleteUser(@PathParam("id") final String id) {
final String res = "{\"success\":true,\"msg\":\"User " + id + " successfully deleted.\"}";
return res;
}
}

ResponseCorsFilter.java

public class ResponseCorsFilter implements ContainerResponseFilter {

@Override
public void filter(final ContainerRequestContext req, final ContainerResponseContext contResp) {

contResp.getHeaders().add("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
contResp.getHeaders().add("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
contResp.getHeaders().add("Access-Control-Allow-Origin", "*");

final String reqHead = req.getHeaderString("Access-Control-Request-Headers");
if ((null != reqHead) && StringUtils.isNotBlank(reqHead)) {
contResp.getHeaders().add("Access-Control-Request-Headers", reqHead);
}

}

}

目前我无法删除内容。在客户端,我从表单面板获取记录并调用 erase function .请求/响应如下所示:

General:
Remote Address:127.0.0.1:8080
Request URL:http://localhost:8080/app/rest/user/user4
Request Method:DELETE
Status Code:400 Bad Request

Response Headers
Connection:close
Content-Length:0
Date:Tue, 14 Apr 2015 19:26:05 GMT

Request Headers
Accept:*/*
Accept-Encoding:gzip, deflate, sdch
Accept-Language:de-DE,de;q=0.8,en-US;q=0.6,en;q=0.4
Connection:keep-alive
Content-Length:14
Content-Type:application/json
Host:localhost:8080
Origin:http://localhost
Referer:http://localhost/app/
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.118 Safari/537.36
X-Requested-With:XMLHttpRequest

Request Payload
{"id":"user4"}

在控制台上,我看到 XMLHttpRequest 无法加载 http://localhost:8080/app/rest/user/user4。请求的资源上不存在“Access-Control-Allow-Origin” header 。因此不允许访问源“http://localhost”。响应具有 HTTP 状态代码 400。

它也可以通过以下 jQuery.ajax() 调用重现:$.ajax({method:'DELETE',url:"http://localhost:8080/app/rest/user/user4",数据:{“id”:“user4”}})

发送没有表单数据或负载的请求是有效的。

有没有另一种方法可以解决这个问题而不覆盖 ExtJS 框架中的内容?

问候
索伦

最佳答案

DELETE 以及 GET 请求不应该有负载(实体主体)。我不知道这是否在任何地方指定,但你可以看到讨论 here .

这就是导致 400 Bad Request 的原因(对于 GET 也会发生)。摆脱它,它就会起作用。但是无论如何,甚至不需要像您一样发送该正文,唯一的信息是 id,它已经包含在 URL 路径中。

如果这只是一个示例,并且您需要随请求发送一些其他任意信息,则使用查询参数,例如

 /app/rest/user/user4?some=value1&other=value2&data=value3

public String deleteUser(@PathParam("id") final String id,
@QueryParam("some") String some,
@QueryParam("other") String other,
@QueryParam("data") String data) {}

使用 jQuery,你可以做到

var params = {
some:"valeu1",
other:"value2",
data:"value3"
}

var encoded = $.param(params);
var url = baseUrl + "?" + encoded;
$.ajax({
url: url,
...
})

更新

经过一番调查,这似乎是 Grizzly 的问题。我已经使用 Jetty 进行了测试,它运行良好。

查看类似问题

  • Here - 最后的评论说它是固定的,但我不能产生一个工作示例
  • Here

更新 2

因此@alexey在下面的评论中说明

You're right, Grizzly by default doesn't allow payload for HTTP methods, for which HTTP spec doesn't explicitly state that. Like HTTP GET, DELETE, HEAD. But you can switch the support on by calling: httpServer.getServerConfiguration().setAllowPayloadForUndefinedHttpMethods(true)‌​; Please not the method should be called before starting HttpServer

所以解决方法是做类似的事情

public static HttpServer createServer() {
final ResourceConfig rc = new ResourceConfig();
rc.packages(true, "app");
rc.register(ResponseCorsFilter.class);

HttpServer server = GrizzlyHttpServerFactory.createHttpServer(
URI.create(BASE_URI), rc, false);
server.getServerConfiguration().setAllowPayloadForUndefinedHttpMethods(true);
return server;
}

public static void main(String[] args) throws IOException {
final HttpServer server = createServer();
server.start();
System.out.println(String.format("Jersey app started with WADL available at "
+ "%sapplication.wadl\nHit enter to stop it...", BASE_URI));
System.in.read();
server.stop();
}

经过测试,它按预期工作。

关于java - 删除带有有效负载或表单数据的请求会导致错误请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29636529/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com