简体   繁体   English

在Java上构建REST API时出现HTTP 404错误

[英]HTTP 404 error while buiding REST API on Java

I am starting with restapi and thought of doing it with a basic program found on internet. 我从restapi开始,并考虑使用Internet上找到的基本程序来进行操作。 Sharing you the code. 分享您的代码。

The class File: 类文件:

package com.java2novice.restful;
     import javax.ws.rs.GET;
    import javax.ws.rs.Path;
    import javax.ws.rs.PathParam;
    import javax.ws.rs.core.Response;

    @Path("/publish")
    public class RestEasyExample {

        @GET
        @Path("/{message}")
        public Response publishMessage(@PathParam("message") String msgStr){

            String responseStr = "Received message: "+msgStr;
            return Response.status(200).entity(responseStr).build();
        }
    }

The web.xml- web.xml-

<web-app id="WebApp_ID" version="2.4"
    xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

    <!-- Auto scan REST service -->
    <context-param>
        <param-name>resteasy.scan</param-name>
        <param-value>true</param-value>
    </context-param>


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

    <servlet-mapping>
        <servlet-name>resteasy-servlet</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>
</web-app>

The pom.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>RestfulWebServices</groupId>
  <artifactId>RestfulWebServices</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  <build>
    <sourceDirectory>src</sourceDirectory>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.3</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
      <plugin>
        <artifactId>maven-war-plugin</artifactId>
        <version>2.6</version>
        <configuration>
          <warSourceDirectory>WebContent</warSourceDirectory>
          <failOnMissingWebXml>false</failOnMissingWebXml>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

I am using the jersey 2.0. 我正在使用球衣2.0。 I have deployed the war and placed it in webapps folder of Tomcat. 我已经部署了战争并将其放置在Tomcat的webapps文件夹中。 I have tried calling it using two urls- 我尝试使用两个网址调用它-

http://localhost:8080/RestfulWebServices/publish/abc

and

http://localhost:8080/RestfulWebServices/publish?message=abc

But i still keep on getting HTTP 404 not found. 但是我仍然继续获取未找到的HTTP 404。

Please assist me in where i am missing something. 请协助我,我想念的东西。

Thanks 谢谢

Try using rest application container with this configuration over web.xml configuration. 尝试通过Web.xml配置将此配置使用其余应用程序容器。

package com.java2novice.config;
import org.glassfish.jersey.server.ResourceConfig;

import java.util.Collection;

@javax.ws.rs.ApplicationPath("v1")
public class ApplicationConfig extends ResourceConfig {

    public ApplicationConfig() {
        // package where all your api lives
        packages("com.java2novice.restful");
    }

    @Override
    public Collection<Strinzg> getPropertyNames() {
        return super.getPropertyNames();
    }
}

Then visit /v1/your-endpoint 然后访问/ v1 /您的端点

I think you are mixing resteasy with jersey. 我认为您正在将运动衫和休闲服混在一起。

In either way, the <listener> is required param in web.xml 无论哪种方式, <listener>都是web.xml必需参数

Try adding following in your web.xml file: 尝试在web.xml文件中添加以下内容:

<listener>
    <listener-class>
        org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap
    </listener-class>
</listener>

You should also consider using restEasy's servlet container rather using Jersey. 您还应该考虑使用restEasy的servlet容器,而不是使用Jersey。

<servlet>
    <servlet-name>resteasy-servlet</servlet-name>
    <servlet-class>
        org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher
    </servlet-class>
</servlet>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM