Search code examples
javajax-rswildflyopenapiresteasy

Why my jaxrs APIs has the application name as application path?


I created a simple version API with rest-easy and wildfly, everything should works but the application path has the name of my project:

  • My version API return the version number from my pom.xml: The URI should be http://localhost:8080/version
    but to acces it this is http://localhost:8080/projectName/version.

  • To acces to the open api file : The URI should be http://localhost:8080/openapi
    but this is http://localhost:8080/projectName/openapi

I tried to extend Application to set the @ApplicationPath("/") but it's not working, it just allowed me to add element on the application path
example: http://localhost:8080/projectName/test/....
How can I set the application path to the root (/) and remove the projectName ?


JaxRSActivator class:
@ApplicationPath("/")
public class JaxRSActivator extends Application {

    public JaxRSActivator()
    {
        super();
    }
}

VersionFacade class:
@Path("/version")
@Tags
public interface VersionFacade {
    @GET
    @Produces(TEXT_PLAIN)
    @Operation(summary = "Application version", 
    responses = {
            @ApiResponse(responseCode = "200", 
                    description = "Version number",
                    content = @Content(mediaType = TEXT_PLAIN, schema = @Schema(implementation = String.class)))})
    String getVersion();
}

web.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_9" version="3.0"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
  http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
  
    <!-- public API -->
    <security-constraint>
        <web-resource-collection>
            <web-resource-name>public</web-resource-name>
            <url-pattern>/openapi.json</url-pattern>
            <url-pattern>/version</url-pattern>
        </web-resource-collection>
    </security-constraint>

    <context-param>
        <param-name>resteasy.role.based.security</param-name>
        <param-value>true</param-value>
    </context-param>
</web-app>

openapi-configuration.yaml file
openapi: 3.0.0
prettyPrint: true
cacheTTL: 0
openAPI:
    info:
        version: '0.0.1'
        title: API config file

Solution

  • Jboss wildfly use the Web module name (projectName.war) as default context root. To set it you need to:

    • Add the WEB-INF/jboss-web.xml file in your Web module:
      (if you don't package your app in an EAR file)
    <jboss-web xmlns="http://www.jboss.com/xml/ns/javaee"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
          http://www.jboss.com/xml/ns/javaee
          http://www.jboss.org/j2ee/schema/jboss-web_5_1.xsd">
        <!-- Set context root to / -->
       <context-root>/</context-root>
    </jboss-web>
    
    • Or add the META-INF/application.xml file in your EAR:
      (if you package your app in an EAR file)
    <?xml version="1.0" encoding="UTF-8"?>
    <application 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/application_8.xsd" version="8">
      <display-name>projectName-ear</display-name>
      <module>
        <web>
          <!--My Web module -->
          <web-uri>projectName-ws.war</web-uri>
          <!-- Set context root to / -->
          <context-root>/</context-root>
        </web>
      </module>
      <module>
        <!-- other EJB module -->
        <ejb>projectName-core.jar</ejb>
      </module>
    </application>
    

    If you use Eclipse, careful it sometimes don't publish your application.xml so you need to put it manually in your Wildfly standalone/deployments folder.