Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to specify mime-mapping using servlet 3.0 java config?

I am using Servlet 3.0 and looking to convert my existing web.xml file to java config. Configuring servlets/filters etc seems to be pretty straight away. I can't figure out how to convert the following mime-mapping. Can anyone help me out?

<mime-mapping>
    <extension>xsd</extension>
    <mime-type>text/xml</mime-type>
</mime-mapping>
like image 201
user2145809 Avatar asked Nov 13 '13 17:11

user2145809


3 Answers

I faced this problem in a Spring Boot application. My solution was to create a class that implements org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer as following:

@Configuration
public class MyMimeMapper implements EmbeddedServletContainerCustomizer {
  @Override
  public void customize(ConfigurableEmbeddedServletContainer container) {
    MimeMappings mappings = new MimeMappings(MimeMappings.DEFAULT);
    mappings.add("xsd", "text/xml; charset=utf-8");
    container.setMimeMappings(mappings);
  }
}
like image 82
Samuli Pahaoja Avatar answered Nov 08 '22 15:11

Samuli Pahaoja


Just write a Filter. e.g. for mime-mapping in web.xml:

<mime-mapping>
    <extension>mht</extension>
    <mime-type>message/rfc822</mime-type>
</mime-mapping>

We can write a Filter instead:

@WebFilter("*.mht")
public class Rfc822Filter implements Filter {

    public void doFilter(ServletRequest req, ServletResponse resp,
            FilterChain chain) throws IOException, ServletException {
        resp.setContentType("message/rfc822");
        chain.doFilter(req, resp);
    }

    ...
}
like image 34
auntyellow Avatar answered Nov 08 '22 14:11

auntyellow


Using spring MVC, this method worked for me.

In the web-context, add this:

public class WebContext implements WebMvcConfigurer {

  @Override
  public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
    configurer.mediaType("xsd", MediaType.TEXT_XML);
  }
}
like image 1
pbthorste Avatar answered Nov 08 '22 16:11

pbthorste