java - 我需要ApplicationConfig.java吗

标签 java rest tomcat jersey

尝试通过项目名称 WebServiceRestful_Server 在 Eclipse 中创建我的第一个简单 Web 服务。 DemoRest类代码:

package ws;

import javax.ws.rs.*;
import javax.ws.rs.core.*;


@Path("demo")
public class DemoRest {

    @GET
    @Path("hello")
    @Produces(MediaType.TEXT_PLAIN)
    public String hello()
    {
        return "Hello world";

    }

}

根据手册,我发现我需要 ApplicationConfig 类。我做到了:

package ws;

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

@javax.ws.rs.ApplicationPath("rest")
public class ApplicationConfig extends Application {

    @Override
    public Set<Class<?>> getClasses() {
        Set<Class<?>> resources = new java.util.HashSet<>();
        addRestResourceClasses(resources);
        return resources;
    }
    private void addRestResourceClasses(Set<Class<?>> resources) {
        //add all resources classes
        resources.add(ws.DemoRest.class);
    }
}

在服务器上运行。但是当我去 http://localhost:8080/WebServiceRestful_Server/rest/demo/hello 我只看到 enter image description here

如何获得“Hello world”?

我需要 ApplicationConfig.java 吗?看起来这段代码没有运行。我试图在两个函数上放置断点,但是当我在项目中以 Debug模式运行时,它们没有发生任何事情。

更新:

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>WebServiceRestful_Server</groupId>
  <artifactId>WebServiceRestful_Server</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  <build>
    <sourceDirectory>src</sourceDirectory>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.5.1</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
      <plugin>
        <artifactId>maven-war-plugin</artifactId>
        <version>3.0.0</version>
        <configuration>
          <warSourceDirectory>WebContent</warSourceDirectory>
        </configuration>
      </plugin>
    </plugins>
  </build>
  <dependencies>
    <dependency>
        <groupId>javax.j2ee</groupId>
        <artifactId>j2ee</artifactId>
    </dependency>
<dependency>
    <groupId>org.glassfish.jersey.containers</groupId>
    <artifactId>jersey-container-servlet</artifactId>
    <version>2.25.1</version>
    <scope>provided</scope>
</dependency>
  </dependencies>
</project>

网络.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>WebServiceRestful_Server</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
</web-app>

最佳答案

如果您可以发布您的 web.xml 和 pom.xml(如果您使用的是 Maven),那么找出问题所在会容易得多。使用 jersey 配置 tomcat 可能很困难,因为有太多方法可以做到这一点。不同的 servlet 和 jersey 版本可以有不同的配置方式。更高版本可以以非常灵活的方式配置,并且会引起很多困惑。

注释 @javax.ws.rs.ApplicationPath("rest") 暗示您正在使用 servlet 3 容器(Tomcat 7 或更高版本)。如果我需要在不查看您的 web.xml 的情况下进行猜测,我会说确保您的 web.xml 配置为使用版本 3,而不是版本 2。

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
...
</web-app>

您甚至可以删除 web-app 标签内的所有内容,jersey 将扫描您的所有文件以找出您的应用程序类和所有资源类(至少用@Path 或@GET 等注释)。 如果它不起作用,两个建议:

  • 如果有时间,看看最新的official document Jersey ,部分 4.7。基于 Servlet 的部署 是您想要阅读的内容。
  • 如果您没有时间,最快的方法是运行此 maven 命令来为您设置一个新项目。这个命令其实是在jersey的website下载jersey的推荐方式.

    mvn archetype:generate -DarchetypeGroupId=org.glassfish.jersey.archetypes -DarchetypeArtifactId=jersey-quickstart-webapp -DarchetypeVersion=2.25.1

温馨提示:运行完上面的maven命令后,在你的web.xml中修改版本为3.0

关于java - 我需要ApplicationConfig.java吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42700750/

相关文章:

javascript - 从快速路线下载图像

tomcat - 我可以从不同的 intelliJ 实例远程调试在 intelliJ 中运行的 tomcat 吗?

tomcat - 无法在 IntelliJ IDEA 中运行 Tomcat

tomcat - 如何仅保护tomcat起始页?

java - 我生成数组元素的所有组合的函数有什么错误?

SpringBoot 使用高级休息客户端进行简单的分段文件上传(Chrome)

c# - 第三方使用的 Azure 函数 REST API - 是否需要使用 ConfigureAwait(false)?

java - 有什么方法可以使用 Apache POI 读取 .xls 和 .xlsx 文件吗?

java - 如何在 Java 中使用 Jericho 查找自定义开始标记?

java - Spring bean后处理器: inject a value