Sör*_*ren 2 java ajax xmlhttprequest grizzly jersey-2.0
我正在使用Java Jersey 2.17构建RESTful Web服务.客户端.我正在使用ExtJS 5进行开发.
我的服务课程
Main.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();
}
}
Run Code Online (Sandbox Code Playgroud)
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;
}
}
Run Code Online (Sandbox Code Playgroud)
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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
目前我一直在删除内容.在客户端,我从表单面板获取记录并调用擦除功能.请求/响应如下所示:
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"}
Run Code Online (Sandbox Code Playgroud)
在控制台上,我看到了 XMLHttpRequest cannot load http://localhost:8080/app/rest/user/user4. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost' is therefore not allowed access. The response had HTTP status code 400.
它也可以通过以下jQuery.ajax()调用重现: $.ajax({method:'DELETE',url:"http://localhost:8080/app/rest/user/user4",data:{"id":"user4"}})
在没有表单数据或有效负载的情况下发送请求正在运行.
有没有其他方法可以解决这个问题,而无需覆盖ExtJS框架中的内容?
问候
Sören
DELETE
,以及GET
请求不应该有有效载荷(实体主体).我不知道这是否在任何地方指定,但你可以在这里看到讨论.
这就是造成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) {}
Run Code Online (Sandbox Code Playgroud)
使用jQuery,你可以做到
var params = {
some:"valeu1",
other:"value2",
data:"value3"
}
var encoded = $.param(params);
var url = baseUrl + "?" + encoded;
$.ajax({
url: url,
...
})
Run Code Online (Sandbox Code Playgroud)
经过一番调查,这似乎是一个灰熊问题.我用Jetty测试过,它工作正常.
看到类似的问题
所以@alexey在下面的评论中说
你是对的,Grizzly默认情况下不允许HTTP方法的有效负载,HTTP规范没有明确说明.像HTTP GET,DELETE,HEAD.但您可以通过调用来切换支持:
httpServer.getServerConfiguration().setAllowPayloadForUndefinedHttpMethods(true)??;
请不要在启动之前调用该方法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();
}
Run Code Online (Sandbox Code Playgroud)
经测试,它按预期工作.
归档时间: |
|
查看次数: |
4227 次 |
最近记录: |