Search code examples
javaundertow

Set default encoding for Undertow embedded


I'm using Undertow for embedded HTTP server in my app. The default encoding is ISO-8859-1 for URL encoding and query parameters. I want to use UTF-8. I did not find anything in documentation. Any idea?


Solution

  • I setup defaultEncoding on DeploynentInfo. It helped me.

    DeploymentInfo servletBuilder =
        Servlets
            .deployment()
            .setClassLoader(Main.class.getClassLoader())
            .setDeploymentName("myapp").setContextPath("/myapp")
            .setDefaultEncoding("UTF-8");
    
    DeploymentManager manager = Servlets.defaultContainer().addDeployment(servletBuilder);
    manager.deploy();
    
    Undertow server = Undertow.builder()
        .addHttpListener(9090, "localhost")
        .setHandler(manager.start())
        .build();
    
    server.start();
    

    For the current version of Undertow default encoding is UTF-8. Also for the non-deploy mode, you can use options to change the default encoding:

    Undertow.builder()
           .setHandler(...)
           .setServerOption(UndertowOptions.URL_CHARSET, "UTF8")
           .addHttpListener(port, host)
           .build()
           .start();