如何在 Wildfly .ear 中拥有多个 jax-rs jar 而不发生冲突?

问题描述 投票:0回答:1

我在 .ear 中有几个项目,前端在 war 中,服务器 .jar 在 ejb 中,我正在测试 jax-rs 的实现,问题是它在 url 中产生冲突,端点路径也有例如,要经历一场战争,可以通过以下方式观看相同的端点: http://localhost:8180/MyExec/warone/projectone/api/zip?zip=12345678 和 http://localhost:8180/MyExec/wartwo/ projectone/api/zip?zip=12345678 o 如果您在不同的冲突中具有相同的端点(如 /zip),例如: http://localhost:8180/MyExec/warone/projectone/api/zip?zip=12345678 和 http://localhost:8180/MyExec/warone/projecttwo/api/zip?zip=12345678

projetctone application @ApplicationPath("/projectone/api/") public class RestApplication extends Application { }

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

我希望能够使用多个项目 .jar jax-rs 并能够正确映射而不发生冲突

java jax-rs wildfly
1个回答
0
投票

项目树

warone
├── pom.xml
└── src
    └── main
        ├── java
        │   └── com
        │       └── example
        │           ├── projectone
        │           │   ├── MyApplication01.java
        │           │   └── Zip01Resource.java
        │           └── projecttwo
        │               ├── MyApplication02.java
        │               └── Zip02Resource.java
        └── webapp
            └── WEB-INF
                └── jboss-web.xml

pom.xml

<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.example</groupId>
    <artifactId>warone</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>

    <properties>
            <maven.compiler.source>1.8</maven.compiler.source>
                <maven.compiler.target>1.8</maven.compiler.target>
                <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
                <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        </properties>
        
    <dependencies>

    <!-- Servlet API -->
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>4.0.1</version>
        <scope>provided</scope>
    </dependency>

    <!-- JSP API -->
    <dependency>
        <groupId>javax.servlet.jsp</groupId>
        <artifactId>javax.servlet.jsp-api</artifactId>
        <version>2.3.3</version>
        <scope>provided</scope>
    </dependency>

    <!-- JSTL (JavaServer Pages Standard Tag Library) -->
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
    </dependency>

    <!-- JAX-RS (Java API for RESTful Web Services) -->
    <dependency>
        <groupId>javax.ws.rs</groupId>
        <artifactId>javax.ws.rs-api</artifactId>
        <version>2.1</version>
    </dependency>
        
    </dependencies>

    <build>
        <finalName>warone</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>3.4.0</version>
                <configuration>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>            
        </plugins>
    </build>
</project>

MyApplication01.java

package com.example.projectone;

import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
import java.util.HashSet;
import java.util.Set;

@ApplicationPath("/projectone/api")
public class MyApplication01 extends Application {

    @Override
    public Set<Class<?>> getClasses() {
        Set<Class<?>> resources = new HashSet<>();
        resources.add(Zip01Resource.class);
        return resources;
    }
}

Zip01Resource.zip

package com.example.projectone;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

@Path("/zip")
public class Zip01Resource {
    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String downloadZip(@QueryParam("zip") String zipName) {
        return "download Zip - ZIP01 -" + zipName ;
    }
}

MyApplication02.java

package com.example.projecttwo;

import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
import java.util.HashSet;
import java.util.Set;

@ApplicationPath("/projecttwo/api")
public class MyApplication02 extends Application {

    @Override
    public Set<Class<?>> getClasses() {
        Set<Class<?>> resources = new HashSet<>();
        resources.add(Zip02Resource.class);
        return resources;
    }
}

Zip02Resource.java

package com.example.projecttwo;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

@Path("/zip")
public class Zip02Resource {
    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String downloadZip(@QueryParam("zip") String zipName) {
        return "download Zip - ZIP02 - " + zipName ;
    }
}

jboss-web.xml

<?xml version="1.0" encoding="UTF-8"?>
<jboss-web>
    <context-root>/MyExec/warone</context-root>
</jboss-web>

套餐

mvn clean package

安装

将你的战争(warone.war)安装到wildfly

测试投影仪

http://127.0.0.1:8080/MyExec/warone/projectone/api/zip?zip=12345678

返回

download Zip - ZIP01 -12345678

测试项目二

http://127.0.0.1:8080/MyExec/warone/projecttwo/api/zip?zip=12345678

返回

download Zip - ZIP02 - 12345678

注意

如果需要调用EJB并返回ZIP文件,请修改 Zip01Resource.java 和 Zip02Resource.java 的内容更改为使用您自己的 EJB,并且返回字符串更改为返回 javax.ws.rs.core.Response。

这是完整的代码,已经在 JDK 8 和 Wildfly 17 上进行了验证和测试。

  • 关键点1:jboss-web.xml设置MyExec/warone

  • 要点2:使用2个应用程序分别设置

    @ApplicationPath("/projectone/api")
    @ApplicationPath("/projecttwo/api")

  • http://localhost:8080/MyExec/warone/projectone/api/zip?zip=12345678

  • http://localhost:8080/MyExec/warone/projecttwo/api/zip?zip=12345678

我的wildfly使用默认端口8080

© www.soinside.com 2019 - 2024. All rights reserved.