WildFly localhost 'forbidden' access
Asked Answered
C

1

1

I am trying to create a web app that complies with the Jakarta RESTful Web Services (formerly JAX-RS) specification, running on a WildFly server.

I can start a deployment and access the WildFly homepage. But when I want to access my resources (e.g. @Path("/health")). I receive a FORBIDDEN error:

screenshot of Forbidden error message

When I looked at the management console I saw that there is no JAX_RS deployedL

enter image description here

I am utterly lost at what to do. Can somebody help?

I have tried adding management and application users, creating a web.xml, creating a extension of Application, creating a CORS filter, updating my pom.xml file.

Chur answered 12/2 at 13:15 Comment(1)
We need to know more about your application. Is it a WAR? Do you have a web.xml file? Do you have a type that extends jakarta.ws.rs.core.Application with a @ApplicationPath annotation? What version of WildFly are you using?Govea
K
0

tl;dr

Insert api into your URL. (Or whatever value is specified in your @ApplicationPath annotation.)

http://localhost:8080/your_final_artifact_name_here/your_ApplicationPath_here/your_Path_here

The parts in that URL:

  • http://
    The usual Web protocol, assuming your are not using TLS ("SSL") encryption (https).
  • localhost:8080
    Assuming you are running the server on your local development machine, with Wildfly configured to listen on port 8080 rather than the Web default of 80.
  • your_final_artifact_name_here
    The name of the final software artifact produced when you build your project, likely a WAR file I suppose. Your Maven POM file may rewrite or append to the name of your project name, such as -1.0-SNAPSHOT, to produce a file artifact name. This value here is the formal name of your web app, your “context” in Jakarta Servlet lingo.
  • your_ApplicationPath_here
    The piece of text you specify in your @AppliationPath annotation. For example, api per the Maven template used by IntelliJ new-project wizard.
  • your_Path_here
    The piece of text you specify in your Path annotation, the name of your REST resource.

Details

Your @Path annotation is only part of the URL needed to access your REST resource. Before that in the URL is the text you specified in your @ApplicationPath annotation on the class that extends Application.

The catch is that part of the URL to the REST resource being accessed defined in the "@ApplicationPathof the class extendingApplication`.

package work.basil.example.exjakartarestserver;

import jakarta.ws.rs.ApplicationPath;
import jakarta.ws.rs.core.Application;

@ApplicationPath ( "/api" )
public class HelloApplication extends Application { }

Notice the /api in that code above. That must appear in your URL.

Here is our REST resource:

package work.basil.example.exjakartarestserver;

import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;

@Path ( "/hello-world" )
public class HelloResource
{
    @GET
    @Produces ( "text/plain" )
    public String hello ( )
    {
        return "Hello, World!";
    }
}

Suppose, as in my case, that:

  • Your project is named ExJakartaRestServer.
  • The Maven config is such that the name of the software artifact produced in your project’s target folder is ExJakartaRestServer-1.0-SNAPSHOT.
  • Your @ApplicationPath value is /api.
  • That @Path value is /hello-world as seen above. This is the name of your “REST resource”.

Then, you can access your REST resource with this URL:

http://localhost:8080/ExJakartaRestServer-1.0-SNAPSHOT/api/hello-world

By the way, the code above was aimed at the Jakarta EE 11 generation of Jakarta RESTful Web Services, Eclipse Jersey, and WildFly.


May not matter, but if curious, here is my POM.

scope of library for Jakarta RESTful Web Services API

Notice the scope element on the dependency for the interfaces of jakarta.ws.rs-api. That scope is provided because we only need a copy of the interfaces while compiling, but we do not want to include them in our final software artifact (our WAR file). Those interfaces will be provided at runtime by the Jakarta EE compliant server on which we deploy (Wildfly in this case here). Jakarta EE compliant server such as Wildfly brings both the API (interfaces) and an implementation of Jakarta RESTful Web Services if that server supports either the Web profile or the “Full” profile of the Jakarta EE specification. Problems might ensue if we brought an additional unneeded copy of the interfaces along in our WAR.

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>work.basil.example</groupId>
    <artifactId>ExJakartaRestServer</artifactId>
    <version>1.0-SNAPSHOT</version>
    <name>ExJakartaRestServer</name>
    <packaging>war</packaging>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.release>22</maven.compiler.release>
    </properties>

    <dependencies>

        <!-- https://mvnrepository.com/artifact/jakarta.ws.rs/jakarta.ws.rs-api -->
        <dependency>
            <groupId>jakarta.ws.rs</groupId>
            <artifactId>jakarta.ws.rs-api</artifactId>
            <version>4.0.0</version>
            <scope>provided</scope>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter -->
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter</artifactId>
            <version>5.11.0</version>
            <scope>test</scope>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>3.4.0</version>
            </plugin>
        </plugins>
    </build>
</project>
Kozak answered 4/9 at 1:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.