在RESTFul Jersey解决方案中,我试图接收包含德语变音字符的URL,例如。
http://localhost:8085/hello/hello/echo/ÄÖÜßööü
当我使用浏览器输入此类URL时,我得到了正确的结果。 当我使用JUnit测试进行测试时,它不起作用。
org.junit.ComparisonFailure: expected:<[ÄÖÜßäöü]> but was:<[ÃÃÃÃäöü]>
at org.junit.Assert.assertEquals(Assert.java:115)
at org.junit.Assert.assertEquals(Assert.java:144)
at com.bitplan.rest.test.TestHello.testUmlaute(TestHello.java:43)
调试输出是:
http://localhost:8085/hello/hello/echo/%C3%84%C3%96%C3%9C%C3%9F%C3%A4%C3%B6%C3%BC
http://localhost:8085/hello/hello/echo/ÄÖÜßäöü
GET:hello/echo/ÃÃÃÃäöü
所以Grizzly / Jersey似乎没有正确处理网址。
我该如何解决这个问题或解决它?
我发现:https://github.com/javaee/grizzly/issues/1377并尝试了不同的编码,例如正如https://stackoverflow.com/a/9542781/1497139中提出的那样,但没有任何区别。
我也设置了
server.getServerConfiguration().setDefaultQueryEncoding(Charsets.UTF8_CHARSET);
如https://github.com/javaee/grizzly/issues/1450所述,但仍然没有运气
@Test
public void testUmlaute() throws Exception {
super.startServer();
URI uri=new URI("http://localhost:8085/hello/hello/echo/ÄÖÜßäöü");
System.out.println(uri.toASCIIString());
System.out.println(uri.toString());
WebResource wrs = Client.create().resource(uri);
String result= wrs.accept("text/html; charset=utf-8").get(String.class);
assertEquals("ÄÖÜßäöü",result);
}
“Hello / Echo”资源
package com.bitplan.hello.resources;
import javax.ws.rs.GET;
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.Request;
import javax.ws.rs.core.UriInfo;
@Path("/hello")
/**
* Simple Hello Resource
* @author wf
*
*/
public class HelloResource {
@Context
UriInfo uriInfo;
@Context
Request request;
@GET
public String getHello() {
return "Hello";
}
@GET
@Produces("text/html")
@Path("echo/{value}")
public String getEcho(@PathParam("value") String value) {
System.out.println(request.getMethod()+":"+uriInfo.getPath());
return value;
}
}
maven依赖
<properties>
<!-- jersey version -->
<!-- <jersey.version>2.22.2</jersey.version> -->
<jersey.version>1.19.1</jersey.version>
<!-- http://grizzly.java.net/ -->
<grizzly.version>2.3.25</grizzly.version>
...
</properties>
...
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-bundle</artifactId>
<version>${jersey.version}</version>
</dependency>
<!-- Grizzly server -->
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-grizzly2</artifactId>
<version>${jersey.version}</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-grizzly2-servlet</artifactId>
<version>${jersey.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.grizzly</groupId>
<artifactId>grizzly-http-server</artifactId>
<version>${grizzly.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.grizzly</groupId>
<artifactId>grizzly-http-servlet</artifactId>
<version>${grizzly.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.grizzly</groupId>
<artifactId>grizzly-http</artifactId>
<version>${grizzly.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish</groupId>
<artifactId>javax.servlet</artifactId>
<version>3.0</version>
<scope>provided</scope>
</dependency>