1

In a RESTFul Jersey solution I am trying to receive URLs that contain German Umlaut characters e.g.

http://localhost:8085/hello/hello/echo/ÄÖÜßööü

When I enter such URLs with a browser I get correct results. When I test things with a JUnit test it doesn't work.

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)

the debug output is:

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/ÃÃÃÃäöü

and so it seems the url is not handled correctly by Grizzly/Jersey.

How can I fix this or work around it?

I found: https://github.com/javaee/grizzly/issues/1377 and tried different encodings e.g. as proposed in https://stackoverflow.com/a/9542781/1497139 but that didn't make any difference.

I also set

server.getServerConfiguration().setDefaultQueryEncoding(Charsets.UTF8_CHARSET);

as outlined in https://github.com/javaee/grizzly/issues/1450 but still no luck

JUnit Test see https://github.com/BITPlan/com.bitplan.simplerest/blob/master/src/test/java/com/bitplan/rest/test/TestHello.java#L42

@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" Resource

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 dependencies

    <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>
Wolfgang Fahl
  • 15,016
  • 11
  • 93
  • 186
  • If you say it works from browser with the same server, doesn't it mean that the problem might be on the client side? – alexey Jun 13 '16 at 18:35
  • Probably so. But the server should be able to handle both the encode and non encoded versions of the url. – Wolfgang Fahl Jun 13 '16 at 20:37
  • Could you pls. share complete testcase, so we can reproduce? – alexey Jun 16 '16 at 00:32
  • what are you missing? – Wolfgang Fahl Jun 16 '16 at 03:42
  • It would be great to have a project on github, check it out and run/test – alexey Jun 17 '16 at 20:06
  • see3 https://github.com/BITPlan/com.bitplan.simplerest and https://github.com/BITPlan/com.bitplan.simplerest-api – Wolfgang Fahl Jun 18 '16 at 04:36
  • The tests work fine for me, no error. From your report I see that there is some problem with encoding, but not sure where exactly, if it's on the client or server side. Are you able to take an HTTP packets dump using Wireshark or tcpdump to see what is getting sent over the wires? – alexey Jun 22 '16 at 00:59

0 Answers0