为什么我的JAXRS API将应用程序名称作为应用程序路径?

发布于 2025-01-30 15:42:27 字数 2498 浏览 4 评论 0原文

我创建了一个带有休息和野生蝇的简单版本API,一切都应该有效,但是应用程序路径的名称是我的项目的名称:

  • 我的版本API从我的pom.xml: URI应为http:// localhost:8080/version
    但是要进行访问,这是http:// localhost:8080/projectName/version

  • 要加入打开API文件:URI应为http:// localhost:8080/OpenAPI
    但这是http:// localhost:8080/projectName/openapi

我尝试扩展应用程序以设置@applicationpath(“/“/”)>允许我在应用程序路径上添加元素
示例:http:// localhost:8080/projectName/test/...
如何将应用程序路径设置为root(/)并删除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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无远思近则忧 2025-02-06 15:42:27

JBoss Wildfly使用Web模块名称(ProjectName.war)作为默认上下文root。要设置它,您需要:

  • 添加Web-Inf/jboss-web.xml在您的Web模块中的文件:

    (如果您不在EAR文件中包装应用程序)
<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>
  • 或在您的耳朵中添加meta-inf/application.xml文件:

    (如果将应用程序包装在ear文件中),
<?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>

如果您使用Eclipse,请小心,有时不会发布您的application.xml,因此您需要手动将其放入Wildfly 独立/部署中文件夹。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文