首页 > 解决方案 > 未找到 JAX-RS

问题描述

我正在用 wildfly 编写简单的 jax-rs 应用程序。我得到一个 404 Not Found 错误:

有课:

package com.trofimovep;

import javax.enterprise.context.ApplicationScoped;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.core.Application;
import javax.ws.rs.core.Response;

@ApplicationScoped
@Path("/he")
public class Start extends Application {
    @GET
    @Path("/books")
    public Response showBooks() {
        String output = "This is a BookShop !";
        return Response.status(200).entity(output).build();
    }
}

jboss-web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<jboss-web version="10.0"
           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_10_0.xsd">

    <context-root>/Bookapp</context-root>
</jboss-web>

pom.xml

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>0</groupId>
  <artifactId>Bookapp</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>war</packaging>

  <name>Book JAX_RS Maven Webapp</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>

    <!-- https://mvnrepository.com/artifact/com.sun.jersey/jersey-server -->
    <dependency>
      <groupId>com.sun.jersey</groupId>
      <artifactId>jersey-server</artifactId>
      <version>1.19.4</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/com.sun.jersey/jersey-servlet -->
    <dependency>
      <groupId>com.sun.jersey</groupId>
      <artifactId>jersey-servlet</artifactId>
      <version>1.19.4</version>
    </dependency>

    <!--&lt;!&ndash; https://mvnrepository.com/artifact/org.glassfish.jersey.containers/jersey-container-servlet-core &ndash;&gt;-->
    <!--<dependency>-->
      <!--<groupId>org.glassfish.jersey.containers</groupId>-->
      <!--<artifactId>jersey-container-servlet-core</artifactId>-->
      <!--<version>2.27</version>-->
    <!--</dependency>-->


    <!-- https://mvnrepository.com/artifact/com.sun.jersey/jersey-client -->
    <dependency>
      <groupId>com.sun.jersey</groupId>
      <artifactId>jersey-client</artifactId>
      <version>1.19.4</version>
    </dependency>

      <!-- https://mvnrepository.com/artifact/javax/javaee-api -->
      <dependency>
          <groupId>javax</groupId>
          <artifactId>javaee-api</artifactId>
          <version>7.0</version>
          <scope>provided</scope>
      </dependency>

    <!-- https://mvnrepository.com/artifact/org.wildfly.swarm/wildfly-swarm-plugin -->
    <dependency>
      <groupId>org.wildfly.swarm</groupId>
      <artifactId>wildfly-swarm-plugin</artifactId>
      <version>2018.5.0</version>
    </dependency>

  </dependencies>

  <build>
    <finalName>Book JAX_RS</finalName>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
      <plugins>
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.0.0</version>
        </plugin>
        <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.7.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.20.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-war-plugin</artifactId>
          <version>3.2.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.5.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.8.2</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>

我尝试了几乎变体:创建了另一个从 Application 扩展的类,删除了 @ApplicationScoped,并且我的所有操作都没有给出结果,它是http://localhost:8080/he/books上的 404 NOT FOUND 。我做错了什么?为什么它不能通过路径看到消息?

标签: javajax-rswildfly

解决方案


我没有看到你在你的 URL 中使用这个

<context-root>/Bookapp</context-root>

所以也许bookapp/he/books会起作用


推荐阅读