Java REST - 找不到 404 资源

我正在尝试访问我的 Hello world(目前)应用程序上的资源。但是,我得到了这个 HTTP 状态 404 - 当我在 Tomcat 上部署它并点击 URL 时找不到它:localhost:8080/test/rest。这是我的 pom.xml 依赖项:


<dependency>

        <groupId>org.glassfish.jersey.core</groupId>

        <artifactId>jersey-common</artifactId>

        <version>2.26-b03</version>

</dependency>

<dependency>

       <groupId>org.glassfish.jersey.core</groupId>

       <artifactId>jersey-server</artifactId>

       <version>2.26-b03</version>

</dependency>

<dependency>

        <groupId>javax.servlet</groupId>

        <artifactId>javax.servlet-api</artifactId>

        <version>3.1.0</version>

</dependency>

<dependency>

        <groupId>org.glassfish.jersey.containers</groupId>

        <artifactId>jersey-container-servlet</artifactId>

        <version>2.26-b03</version>

</dependency>

<dependency>

       <groupId>junit</groupId>

       <artifactId>junit</artifactId>

       <version>4.11</version>

       <scope>test</scope>

</dependency>

这是我的 web.xml 配置:


<web-app>

    <display-name>Archetype Created Web Application</display-name>

<servlet>

    <servlet-name>HelloWorld Jersey Service </servlet-name>

    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>

<init-param>

    <param-name >jersey.config.server.provider.packages</param-name>

    <param-value >com.latin.translator.app</param-value>

</init-param>

<load-on-startup>1</load-on-startup>

</servlet>

<servlet-mapping>

    <servlet-name >HelloWorld Jersey Service </servlet-name >

    <url-pattern >/test/*</url-pattern >

</servlet-mapping>

</web-app>

Java代码是:


@Path("/test")

public class Test {


    @GET

    @Produces(TEXT_PLAIN)

    @Path("/rest")

    public String test() {

        return "Great success";

    }

}

你有什么想法我做错了什么吗?


九州编程
浏览 157回答 3
3回答

宝慕林4294392

尝试本地主机:8080/test/test/rest<servlet-mapping>&nbsp; &nbsp; <servlet-name >HelloWorld Jersey Service </servlet-name >&nbsp; &nbsp; <url-pattern >/test/*</url-pattern ></servlet-mapping>这表示 root 是 test 所以 url 是 localhost:8080/test/ for jesrsey现在@Path("/test")public class Test {说现在下一个 url 路径是 test 所以 url 是 localhost:8080/test/test@Path("/rest")&nbsp; &nbsp; public String test() {&nbsp; &nbsp; &nbsp; &nbsp; return "Great success";&nbsp; &nbsp; }说 /rest 作为下一个 url 所以它的 localhost:8080/test/test/rest

收到一只叮咚

您需要扩展javax.ws.rs.core.Application该类。@ApplicationPath("/") // the context root of you applicationpublic class JaxRsConfig extends Application {&nbsp; &nbsp; &nbsp; private final Set<Class<?>> classes;&nbsp; &nbsp; &nbsp; public JaxRsConfig() {&nbsp; &nbsp; &nbsp; &nbsp; HashSet<Class<?>> c = new HashSet<>();&nbsp; &nbsp; &nbsp; &nbsp; c.add(Test.class); //repeat for all JAX-RS classes in your application&nbsp; &nbsp; &nbsp; &nbsp; classes = Collections.unmodifiableSet(c);&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; public Set<Class<?>> getClasses() {&nbsp; &nbsp; &nbsp; &nbsp; return classes;&nbsp; &nbsp; &nbsp; }}有了这个,您就不需要 web.xml 文件了。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java