How to set Content-Type for automatically delivered file in GlassFish?

When you deploy the .war file to the GlassFish server (currently in 4.1), the GF server automatically sends files from the WEB-INF/ folder. (If I do not redefine their addresses by specifying a servlet, that is.)

In fact, when you deploy a .war file to a GF server, it extracts files under WEB-INF/ up to {gf-home}/glassfish/domains/domain1/applications/{app-name} . Then it delivers them when accessing http://{hostname}:8080/{app-name}/{path} .

Now, when accessing .json files, the server does NOT send the HTTP header Content-Type: application/json . This causes the page to not load properly, the FireFox console shows me the XML Parsing Error: not well-formed exception, although the contents of the file are exactly the same. Therefore, I assume this is a missing Content-Type tag.

How can I change this mime-mapping for the application / project itself?

From the pages I've seen so far, you can override this behavior in the {gf-home}/glassfish/domains/domain1/default-web.xml , defining mime-mapping . But, believing that I cannot access this file, download only .war files. Is there a solution? Is it possible to pack default-web.xml somewhere in a .war file?

Another solution that I can think of at the moment is to redefine the addresses of specific .json files with a servlet and add a Content-Type header in Java. But I'm not sure if there is a reliable way to access and read .json files at runtime, but not moving them anywhere in the Java source code, but leaving them in the WEB-INF / folder? Any suggestions?

+5
source share
1 answer

How can I change this mime-mapping for the application / project itself?

By declaring <mime-mapping> entries in your own web application /WEB-INF/web.xml .

Starting with Servlet 3.0, the web.xml become optional. This may explain why you did not find anyone. You can just put your own in webapp. GlassFish 4.1 is a container compatible with servlets 3.1, so you should start working with the following servlet 3.1 web.xml :

 <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> <!-- Config here. --> </web-app> 

In your specific case, you will need the following mime mapping:

  <mime-mapping> <extension>json</extension> <mime-type>application/json</mime-type> </mime-mapping> 

See also:

+6
source

Source: https://habr.com/ru/post/1273769/


All Articles