Java 球衣1.9@带斜线的路径
我有一个令人遗憾的任务,就是试图通过url参数中的斜杠(遗留应用程序) 我已经在2.0+中阅读了关于如何解决这个问题的答案: 但是,以下解决方案在1.9中不起作用: @路径(“{id}/{emailAddress:(.+)?}”) 测试路径:5/1/part2@example.org 这将返回404,表示找不到匹配的路由 仅供参考,这是通过URL编码发送的,但我们的容器(Tomcat)会在Jersey处理之前自动解码 编辑:原来整个请求被Apache的Java 球衣1.9@带斜线的路径,java,jersey,jax-rs,Java,Jersey,Jax Rs,我有一个令人遗憾的任务,就是试图通过url参数中的斜杠(遗留应用程序) 我已经在2.0+中阅读了关于如何解决这个问题的答案: 但是,以下解决方案在1.9中不起作用: @路径(“{id}/{emailAddress:(.+)?}”) 测试路径:5/1/part2@example.org 这将返回404,表示找不到匹配的路由 仅供参考,这是通过URL编码发送的,但我们的容器(Tomcat)会在Jersey处理之前自动解码 编辑:原来整个请求被Apache的allowencodedslass指令阻止了
allowencodedslass
指令阻止了。
相关:根据测试后的注释(注意:使用独立的grizzly容器,而不是tomcat)
独立服务器
import com.sun.jersey.api.container.grizzly2.GrizzlyServerFactory;
import com.sun.jersey.api.core.PackagesResourceConfig;
import com.sun.jersey.api.core.ResourceConfig;
import org.glassfish.grizzly.http.server.HttpServer;
import javax.ws.rs.core.UriBuilder;
import java.io.IOException;
import java.net.URI;
public class Main {
private static int getPort(int defaultPort) {
//grab port from environment, otherwise fall back to default port 9998
String httpPort = System.getProperty("jersey.test.port");
if (null != httpPort) {
try {
return Integer.parseInt(httpPort);
} catch (NumberFormatException e) {
}
}
return defaultPort;
}
private static URI getBaseURI() {
return UriBuilder.fromUri("http://localhost/api").port(getPort(8080)).build();
}
public static final URI BASE_URI = getBaseURI();
protected static HttpServer startServer() throws IOException {
ResourceConfig resourceConfig
= new PackagesResourceConfig("jersey1.stackoverflow.standalone");
System.out.println("Starting grizzly2...");
return GrizzlyServerFactory.createHttpServer(BASE_URI, resourceConfig);
}
public static void main(String[] args) throws IOException {
// Grizzly 2 initialization
HttpServer httpServer = startServer();
System.out.println(String.format("Jersey app started with WADL available at "
+ "%sapplication.wadl\nHit enter to stop it...",
BASE_URI));
System.in.read();
httpServer.stop();
}
}
注意:资源类位于jersey1.stackoverflow.standalone
中。要运行服务器,只需运行Main
类。资源类将基于new PackagesResourceConfig(“jersey1.stackoverflow.standalone”)
使用此依赖项
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-grizzly2</artifactId>
<version>${jersey-version}</version> <!-- 1.9 -->
</dependency>
卷曲
结果:
id: 5
email: emailpart1/part2@example.org
带有该URL的
@Path
,对我来说,使用1很好。18@peeskillet:我担心有人会这样说:)如果这是一个旧的Jersey错误,我将打破一些东西。刚刚用1.9测试过,也可以:-(@peeskillet:你能提供你的代码示例吗?是的,我会发布它,但请注意,我只是用独立的grizzly容器进行了测试,而不是用tomcat。哇,这个例子对我很有用,但不是我用tomcat运行的那部分。我检查了请求,它附带了%2F,但它只给了我“在此服务器上找不到请求的URL/。/id/电子邮件”不确定要查找其他位置。结果发现Apache阻止了它:
Group id: com.sun.jersey.archetypes
Artifact id: jersey-quickstart-grizzly2
Version: 1.9
curl -v http://localhost:8080/api/email/5/emailpart1/part2@example.org
id: 5
email: emailpart1/part2@example.org