0%

deploy ejb with restful service to jboss

Wildfly: 18.0.1.Final

JDK: 11.0.2

Gradle: 5.6.2

Maven: 3.6.2

We’ll deploy an ear package with two war packages(One of them uses the rest service) to jboss.

Source code

Here is the project structure

1571834311583

The base is module ejb

Web and app modules both depend on ejb

ear includes web and app

root build.gradle

1
group 'individual.cc'
2
version '1.0-SNAPSHOT'
3
4
allprojects {
5
    repositories {
6
        jcenter()
7
        mavenCentral()
8
    }
9
}
10
11
subprojects {
12
    group 'individual.cc'
13
    version '1.0-SNAPSHOT'
14
}

ejb module: build.gradle

1
plugins {
2
    id 'java'
3
}
4
5
sourceCompatibility = 11
6
7
dependencies {
8
    compileOnly 'javax:javaee-api:8.0.1'
9
}

ejb module: session bean

1
package individual.cc.jar.bean.session;
2
3
import javax.ejb.Stateless;
4
5
@Stateless
6
public class SimpleStatelessEjb {
7
    public String hello() {
8
        return "hello world, EJB";
9
    }
10
}

web module: build.gradle

1
plugins {
2
    id 'war'
3
}
4
5
sourceCompatibility = 11
6
7
dependencies {
8
    providedCompile project(':ejb')
9
10
    compileOnly 'javax:javaee-api:8.0.1'
11
}

web module: web servlet

1
package individual.cc.web.servlet;
2
3
import individual.cc.jar.bean.session.SimpleStatelessEjb;
4
5
import javax.ejb.EJB;
6
import javax.servlet.annotation.WebServlet;
7
import javax.servlet.http.HttpServlet;
8
import javax.servlet.http.HttpServletRequest;
9
import javax.servlet.http.HttpServletResponse;
10
import java.io.IOException;
11
import java.io.PrintWriter;
12
13
@WebServlet({"/", "/ejbServlet"})
14
public class EjbServlet extends HttpServlet {
15
    private static final long serialVersionUID = 1L;
16
17
    @EJB
18
    SimpleStatelessEjb statelessBean;
19
20
    @Override
21
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
22
        PrintWriter writer = resp.getWriter();
23
        String msg = statelessBean.hello();
24
        writer.println(msg);
25
    }
26
}

app module: build.gradle

1
plugins {
2
    id 'war'
3
}
4
5
sourceCompatibility = 11
6
7
ext {
8
    jerseyVersion = '2.29.1'
9
}
10
11
dependencies {
12
    providedCompile project(':ejb')
13
    compileOnly 'javax:javaee-api:8.0.1'
14
15
    implementation "org.glassfish.jersey.containers:jersey-container-servlet:${jerseyVersion}"
16
}

app module: controller and rest configuration

1
package individual.cc.app.servlet;
2
3
import individual.cc.jar.bean.session.SimpleStatelessEjb;
4
5
import javax.ejb.EJB;
6
import javax.ejb.Stateless;
7
import javax.ws.rs.GET;
8
import javax.ws.rs.Path;
9
import javax.ws.rs.Produces;
10
import javax.ws.rs.core.MediaType;
11
12
13
@Stateless
14
@Path("hello")
15
public class HelloController {
16
17
    @EJB
18
    SimpleStatelessEjb statelessBean;
19
20
    @GET
21
    @Produces(MediaType.APPLICATION_JSON)
22
    public String hello() {
23
        return statelessBean.hello();
24
    }
25
}
1
package individual.cc.app.configuration;
2
3
import individual.cc.app.servlet.HelloController;
4
import org.glassfish.jersey.server.ResourceConfig;
5
6
import javax.ws.rs.ApplicationPath;
7
8
/**
9
 * 'services', '/services', or '/services/*'
10
 * is all the same. Jersey will change it to be '/services/*'
11
 * <==>
12
 * <servlet-mapping>
13
 * <servlet-name>RestApplication</servlet-name>
14
 * <url-pattern>/services/*</url-pattern>
15
 * </servlet-mapping>
16
 * <p>
17
 * Here with the @ApplicationPath, it's just like if we configured the servlet mapping in the web.xml
18
 */
19
@ApplicationPath("services")
20
public class RestApplication extends ResourceConfig {
21
22
    public RestApplication() {
23
//        packages("individual.cc.app.servlet");
24
        register(HelloController.class);
25
    }
26
}

ear module: build.gradle

1
plugins {
2
    id 'ear'
3
}
4
5
dependencies {
6
    // The following dependencies will be the ear modules and
7
    // will be placed in the ear root
8
    deploy project(':ejb')
9
    deploy project(path: ':web', configuration: 'archives')
10
    deploy project(path: ':app', configuration: 'archives')
11
}
12
13
ear {
14
    appDirName 'src/main/app'  // use application metadata found in this folder
15
    // put dependent libraries into APP-INF/lib inside the generated EAR
16
    libDirName 'APP-INF/lib'
17
    deploymentDescriptor {  // custom entries for application.xml:
18
        initializeInOrder = true
19
    }
20
}

build & deploy

  1. clean and build ejb module
  2. clean and build app/web module
  3. clean and build ear module

Copy ear module/build/libs/ear-1.0-SNAPSHOT.ear to JBOSS HOME/standalone/deployments

Run JBOSS HOME/bin/standalone.bat as administrator

http://127.0.0.1:8080/web-1.0-SNAPSHOT/

http://127.0.0.1:8080/app-1.0-SNAPSHOT/services/hello

https://127.0.0.1:8443/web-1.0-SNAPSHOT/

https://127.0.0.1:8443/app-1.0-SNAPSHOT/services/hello

All of the above will output hello world, EJB



if maven, replace 5 build.gradle

root pom.xml

1
<?xml version="1.0" encoding="UTF-8"?>
2
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3
         xmlns="http://maven.apache.org/POM/4.0.0"
4
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5
    <modelVersion>4.0.0</modelVersion>
6
7
    <groupId>individual.cc</groupId>
8
    <artifactId>j2ee</artifactId>
9
    <packaging>pom</packaging>
10
    <version>1.0-SNAPSHOT</version>
11
    <modules>
12
        <module>ear</module>
13
        <module>ejb</module>
14
        <module>web</module>
15
        <module>app</module>
16
    </modules>
17
18
    <properties>
19
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
20
        <maven.compiler.source>11</maven.compiler.source>
21
        <maven.compiler.target>11</maven.compiler.target>
22
    </properties>
23
24
    <dependencyManagement>
25
        <dependencies>
26
            <!--custom package-->
27
            <dependency>
28
                <groupId>individual.cc</groupId>
29
                <artifactId>ejb</artifactId>
30
                <version>1.0-SNAPSHOT</version>
31
                <type>ejb</type>
32
            </dependency>
33
            <dependency>
34
                <groupId>individual.cc</groupId>
35
                <artifactId>web</artifactId>
36
                <version>1.0-SNAPSHOT</version>
37
                <type>war</type>
38
            </dependency>
39
            <dependency>
40
                <groupId>individual.cc</groupId>
41
                <artifactId>app</artifactId>
42
                <version>1.0-SNAPSHOT</version>
43
                <type>war</type>
44
            </dependency>
45
            <dependency>
46
                <groupId>individual.cc</groupId>
47
                <artifactId>ear</artifactId>
48
                <version>1.0-SNAPSHOT</version>
49
                <type>ear</type>
50
            </dependency>
51
52
            <!--external package-->
53
            <dependency>
54
                <groupId>javax</groupId>
55
                <artifactId>javaee-api</artifactId>
56
                <version>8.0.1</version>
57
            </dependency>
58
59
            <dependency>
60
                <groupId>org.glassfish.jersey.containers</groupId>
61
                <artifactId>jersey-container-servlet</artifactId>
62
                <version>2.29.1</version>
63
            </dependency>
64
        </dependencies>
65
    </dependencyManagement>
66
67
    <build>
68
        <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
69
            <plugins>
70
                <plugin>
71
                    <artifactId>maven-clean-plugin</artifactId>
72
                    <version>3.1.0</version>
73
                </plugin>
74
                <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
75
                <plugin>
76
                    <artifactId>maven-resources-plugin</artifactId>
77
                    <version>3.0.2</version>
78
                </plugin>
79
                <plugin>
80
                    <artifactId>maven-compiler-plugin</artifactId>
81
                    <version>3.8.0</version>
82
                </plugin>
83
                <plugin>
84
                    <artifactId>maven-surefire-plugin</artifactId>
85
                    <version>2.22.1</version>
86
                </plugin>
87
                <plugin>
88
                    <artifactId>maven-jar-plugin</artifactId>
89
                    <version>3.0.2</version>
90
                </plugin>
91
                <plugin>
92
                    <artifactId>maven-war-plugin</artifactId>
93
                    <version>3.2.2</version>
94
                    <configuration>
95
                        <failOnMissingWebXml>false</failOnMissingWebXml>
96
                    </configuration>
97
                </plugin>
98
                <plugin>
99
                    <artifactId>maven-ear-plugin</artifactId>
100
                    <version>3.0.1</version>
101
                </plugin>
102
                <plugin>
103
                    <artifactId>maven-ejb-plugin</artifactId>
104
                    <version>3.0.1</version>
105
                </plugin>
106
                <plugin>
107
                    <artifactId>maven-install-plugin</artifactId>
108
                    <version>2.5.2</version>
109
                </plugin>
110
                <plugin>
111
                    <artifactId>maven-deploy-plugin</artifactId>
112
                    <version>2.8.2</version>
113
                </plugin>
114
                <plugin>
115
                    <artifactId>maven-javadoc-plugin</artifactId>
116
                    <version>3.0.0</version>
117
                </plugin>
118
            </plugins>
119
        </pluginManagement>
120
    </build>
121
</project>

ejb module: pom.xml

1
<?xml version="1.0" encoding="UTF-8"?>
2
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3
         xmlns="http://maven.apache.org/POM/4.0.0"
4
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5
    <modelVersion>4.0.0</modelVersion>
6
7
    <parent>
8
        <groupId>individual.cc</groupId>
9
        <artifactId>j2ee</artifactId>
10
        <version>1.0-SNAPSHOT</version>
11
    </parent>
12
13
    <artifactId>ejb</artifactId>
14
    <packaging>ejb</packaging>
15
16
    <dependencies>
17
        <dependency>
18
            <groupId>javax</groupId>
19
            <artifactId>javaee-api</artifactId>
20
        </dependency>
21
    </dependencies>
22
23
    <build>
24
        <plugins>
25
            <plugin>
26
                <artifactId>maven-ejb-plugin</artifactId>
27
            </plugin>
28
        </plugins>
29
    </build>
30
31
</project>

web module: pom.xml

1
<?xml version="1.0" encoding="UTF-8"?>
2
<project xmlns="http://maven.apache.org/POM/4.0.0"
3
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5
    <modelVersion>4.0.0</modelVersion>
6
7
    <parent>
8
        <groupId>individual.cc</groupId>
9
        <artifactId>j2ee</artifactId>
10
        <version>1.0-SNAPSHOT</version>
11
    </parent>
12
13
    <artifactId>web</artifactId>
14
    <packaging>war</packaging>
15
16
    <dependencies>
17
        <!--custom package-->
18
        <dependency>
19
            <groupId>individual.cc</groupId>
20
            <artifactId>ejb</artifactId>
21
            <type>ejb</type>
22
        </dependency>
23
    </dependencies>
24
25
    <build>
26
        <plugins>
27
            <plugin>
28
                <artifactId>maven-war-plugin</artifactId>
29
                <configuration>
30
                    <failOnMissingWebXml>false</failOnMissingWebXml>
31
                </configuration>
32
            </plugin>
33
        </plugins>
34
    </build>
35
36
    
37
</project>

app module: pom.xml

1
<?xml version="1.0" encoding="UTF-8"?>
2
<project xmlns="http://maven.apache.org/POM/4.0.0"
3
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5
    <modelVersion>4.0.0</modelVersion>
6
7
    <parent>
8
        <groupId>individual.cc</groupId>
9
        <artifactId>j2ee</artifactId>
10
        <version>1.0-SNAPSHOT</version>
11
    </parent>
12
13
    <artifactId>app</artifactId>
14
    <packaging>war</packaging>
15
16
    <dependencies>
17
        <!--custom package-->
18
        <dependency>
19
            <groupId>individual.cc</groupId>
20
            <artifactId>ejb</artifactId>
21
            <type>ejb</type>
22
        </dependency>
23
24
        <dependency>
25
            <groupId>org.glassfish.jersey.containers</groupId>
26
            <artifactId>jersey-container-servlet</artifactId>
27
        </dependency>
28
    </dependencies>
29
30
    <build>
31
        <plugins>
32
            <plugin>
33
                <artifactId>maven-war-plugin</artifactId>
34
            </plugin>
35
        </plugins>
36
    </build>
37
38
    
39
</project>

ear module: pom.xml

1
<?xml version="1.0" encoding="UTF-8"?>
2
<project xmlns="http://maven.apache.org/POM/4.0.0"
3
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5
    <modelVersion>4.0.0</modelVersion>
6
7
    <parent>
8
        <groupId>individual.cc</groupId>
9
        <artifactId>j2ee</artifactId>
10
        <version>1.0-SNAPSHOT</version>
11
    </parent>
12
13
    <artifactId>ear</artifactId>
14
    <packaging>ear</packaging>
15
16
    <dependencies>
17
        <dependency>
18
            <groupId>individual.cc</groupId>
19
            <artifactId>web</artifactId>
20
            <type>war</type>
21
        </dependency>
22
        <dependency>
23
            <groupId>individual.cc</groupId>
24
            <artifactId>app</artifactId>
25
            <type>war</type>
26
        </dependency>
27
    </dependencies>
28
29
    <build>
30
        <plugins>
31
            <plugin>
32
                <artifactId>maven-ear-plugin</artifactId>
33
                <configuration>
34
                    <!--<initializeInOrder>true</initializeInOrder>-->
35
                    <modules>
36
                        <webModule>
37
                            <groupId>individual.cc</groupId>
38
                            <artifactId>web</artifactId>
39
                            <!--MUST reset the name of a package what is in ear package-->
40
                            <bundleFileName>web-in-ear.war</bundleFileName>
41
                            <!--set custom context root-->
42
                            <contextRoot>/web</contextRoot>
43
                        </webModule>
44
                        <webModule>
45
                            <groupId>individual.cc</groupId>
46
                            <artifactId>app</artifactId>
47
                            <!--MUST reset the name of a package what is in ear package-->
48
                            <bundleFileName>app-in-ear.war</bundleFileName>
49
                            <!--set custom context root-->
50
                            <contextRoot>/app</contextRoot>
51
                        </webModule>
52
                    </modules>
53
                </configuration>
54
            </plugin>
55
        </plugins>
56
    </build>
57
58
</project>

build & deploy

clean and install under root pom.xml

Then deploy it to jboss, you can access the following urls:

http://localhost:8080/web/

http://localhost:8080/app/services/hello

https://localhost:8443/web/

https://localhost:8443/app/services/hello