Just Read The Instructions

spaceX drone ship

To have a plain HTTP end-point in Java Servlet is simple easy. Have a class extend HttpServlet and override doGet method. Get writer from HttpServletResponse and call write method on it. Below I wanted to send characters that have accent by setting charset to utf-8

@WebServlet(urlPatterns = { "/test" })
public class TestServlet extends HttpServlet {  
  public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
    PrintWriter out = res.getWriter();
    res.setContentType("application/json; charset=utf-8");
    out.write("le débat sur les armes à feu doit changer");
  }
}

Ok, let's see what the output from a cURL call.

> GET /test HTTP/1.1
> Host: breaking.iavian.net
> User-Agent: curl/7.43.0
> Accept: */*
>
< HTTP/1.1 200 OK  
< Server: nginx  
< Date: Fri, 17 Jun 2016 05:46:05 GMT  
< Content-Type: application/json; charset=ISO-8859-1  
< Content-Length: 41  
< Connection: close  
<  
* Closing connection 0
le d�bat sur les armes � feu doit changer  

The resulting charset is ISO-8859-1, which is not utf-8 set by res.setContentType("application/json; charset=utf-8"), resulting in character mangling. Clearly the servlet doesn't honour setContentType method call. I tried crazy method calls out.write(new String("le débat sur les armes à feu doit changer".getBytes("utf-8"))). No luck, What's wrong?

Always, Just read the instructions. Here is what javadoc for setContentType

     * This method has no effect if called after the response has been committed. It does not set the response's character encoding if it is called after getWriter has been called or after the response has been committed.

Hmm, simple. Moved the call setContentType before calling getWriter.

> GET /test HTTP/1.1
> Host: breaking.iavian.net
> User-Agent: curl/7.43.0
> Accept: */*
>
< HTTP/1.1 200 OK  
< Server: nginx  
< Date: Fri, 17 Jun 2016 06:09:29 GMT  
< Content-Type: application/json; charset=utf-8  
< Content-Length: 43  
< Connection: close  
<  
* Closing connection 0
le débat sur les armes à feu doit changer  

HTTP response is just as expected with proper Charset and no character mangling. Always, Just Read The Instructions