错误404 Jersey Tomcat Eclipse Web服务

时间:2017-08-27 14:56:53

标签: java eclipse rest tomcat glassfish

这可能看起来很复杂,但我发誓我已经完成了Stack溢出的每一个答案,似乎没有什么对我有用。 我正在Eclipse Neon中使用Jersey制作文件上传Web服务。

服务器:我的PC上有Tomcat 8.5(它在C中,我可以将WAR文件放在webapps中并运行它)我在我的日食上有GlassFish Server 4.

当我在任何服务器上运行我的应用程序时,它会找不到错误404:

enter image description here

我的Package Explorer如下所示:

enter image description here

之前,我将org.glassfish servlet容器名称放在web.xml中,所以有时它最终会将错误判断为错误500,这是Classnot Found,我明白这是因为当你有球衣2时glassfish参考有效.X

我不明白我老实说错了什么。我现在真的会拉出我的头发。我已经尝试了一个星期。有人请让我免于精神崩溃。我可能是一个Java菜鸟,但我在几天内提交了一份文章,我的毕业生就开始了。帮助!

的web.xml



<?xml version = "1.0" encoding = "UTF-8"?> 
<web-app xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"  
   xmlns = "http://java.sun.com/xml/ns/javaee"  
   xsi:schemaLocation="http://java.sun.com/xml/ns/javaee  
   http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"  
   id = "WebApp_ID" version = "3.0"> 
   <display-name>File Upload</display-name> 
   <servlet> 
      <servlet-name>Jersey RESTful Application</servlet-name> 
      <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer </servlet-class> 
      <init-param> 
         <param-name>com.sun.jersey.config.property.packages</param-name> 
         <param-value>com.tutorialspoint</param-value> 
      </init-param> 
   </servlet> 
   <servlet-mapping> 
      <servlet-name>Jersey RESTful Application</servlet-name> 
      <url-pattern>/rest/*</url-pattern> 
   </servlet-mapping>   
</web-app>
&#13;
&#13;
&#13;

Java类实现

&#13;
&#13;
package com.quratulaind.us3;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.core.MediaType;
import com.sun.jersey.core.header.FormDataContentDisposition; //jersey-core-1.19.4
import com.sun.jersey.multipart.FormDataParam; //jersey multipart jar
@Path("/cv")

public class us3 {
	@POST
	@Path("/upload")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public String uploadCV(
	@FormDataParam("file") InputStream uploadedInputStream,
	@FormDataParam("file") FormDataContentDisposition fileDetail)
{//saving the file object to the local disk
saveToDisk(uploadedInputStream, fileDetail);
return "File Uploaded Successfully";
}//function uploadcv ends

//Fuction to save uploaded file to disk
private void saveToDisk(InputStream uploadedInputStream, FormDataContentDisposition fileDetail)
{String uploadedFileLocation = "D:\\University Study Stuff\\FYP\\Uploaded CVs For Project"+fileDetail.getFileName();
try{
OutputStream out= new FileOutputStream(new File( uploadedFileLocation));
int read=0;
byte[] bytes= new byte[1024];
out=new FileOutputStream( new File(uploadedFileLocation));
while((read=uploadedInputStream.read(bytes))!= -1){
	out.write(bytes,0,read);
}
out.flush();
out.close();

}//try ends
catch(IOException e)
{e.printStackTrace();}

}//Class ends


}
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

您的项目中看起来jersey版本不匹配。我建议按以下步骤进行操作

删除所有手动添加的引用库。我们将使用maven来管理依赖项。

使用以下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/maven-v4_0_0.xsd">

  <modelVersion>4.0.0</modelVersion>

  <groupId>com.tutorialspoint</groupId>
  <artifactId>assignment</artifactId>
  <packaging>war</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>assignment</name>

  <build>
    <finalName>assignment</finalName>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>2.5.1</version>
        <inherited>true</inherited>
        <configuration>
          <source>1.7</source>
          <target>1.7</target>
        </configuration>
      </plugin>
    </plugins>
  </build>

  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>org.glassfish.jersey</groupId>
        <artifactId>jersey-bom</artifactId>
        <version>${jersey.version}</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
    </dependencies>
  </dependencyManagement>

  <dependencies>
    <dependency>
      <groupId>org.glassfish.jersey.containers</groupId>
      <artifactId>jersey-container-servlet</artifactId>
    </dependency>
    <dependency>
      <groupId>org.glassfish.jersey.inject</groupId>
      <artifactId>jersey-hk2</artifactId>
    </dependency>
    <!-- uncomment this to get JSON support
    <dependency>
      <groupId>org.glassfish.jersey.media</groupId>
      <artifactId>jersey-media-json-binding</artifactId>
    </dependency>
    -->
    <dependency>
      <groupId>org.glassfish.jersey.media</groupId>
      <artifactId>jersey-media-multipart</artifactId>
    </dependency>
  </dependencies>
  <properties>
    <jersey.version>2.26-b09</jersey.version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
</project>

更新web.xml,如下所示

<?xml version="1.0" encoding="UTF-8"?>
<!-- This web.xml file is not required when using Servlet 3.0 
container, see implementation details 
http://jersey.java.net/nonav/documentation/latest/jax-rs.html -->
<web-app version="2.5" 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_2_5.xsd">
  <servlet>
    <servlet-name>Jersey Web Application</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.quratulaind.us3<param-value>
    </init-param>
    <init-param>
      <param-name>jersey.config.server.provider.classnames</param-name>
      <param-value>org.glassfish.jersey.filter.LoggingFilter;org.glassfish.jersey.media.multipart.MultiPartFeature</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>Jersey Web Application</servlet-name>
    <url-pattern>/rest/*</url-pattern>
  </servlet-mapping>
</web-app>

删除资源文件并添加一个

package com.quratulaind.us3;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces; 
import javax.ws.rs.core.MediaType;

import org.glassfish.jersey.media.multipart.FormDataContentDisposition;
import org.glassfish.jersey.media.multipart.FormDataParam;

/**
 * Root resource (exposed at "myresource" path)
 */
@Path("cv")
public class MyResource {

    /**
     * Method handling HTTP GET requests. The returned object will be sent
     * to the client as "text/plain" media type.
     *
     * @return String that will be returned as a text/plain response.
     */
     @GET
     @Produces(MediaType.TEXT_PLAIN)
     public String getIt() {
         return "Got it!";
     }

     @POST
     @Path("/upload")
     @Consumes(MediaType.MULTIPART_FORM_DATA)
     public String uploadCV(
         @FormDataParam("file") InputStream uploadedInputStream,
         @FormDataParam("file") FormDataContentDisposition fileDetail) 
     {
         //saving the file object to the local disk
         saveToDisk(uploadedInputStream, fileDetail);
         return "File Uploaded Successfully";
     }//function uploadcv ends

     //Fuction to save uploaded file to disk
     private void saveToDisk(InputStream uploadedInputStream, 
         FormDataContentDisposition fileDetail) {
         String uploadedFileLocation = "D:\\University Study Stuff\\FYP\\Uploaded CVs For Project"+fileDetail.getFileName();
         try{
             OutputStream out= new FileOutputStream(new File( uploadedFileLocation));
             int read=0;
             byte[] bytes= new byte[1024];
             out=new FileOutputStream( new File(uploadedFileLocation));
             while((read=uploadedInputStream.read(bytes))!= -1){
                 out.write(bytes,0,read);
             }
             out.flush();
             out.close();

          }//try ends
          catch(IOException e)
          {e.printStackTrace();}
      }
  }

完成这些更改后,使用mvn package创建一个war包并部署/复制到${TOMCAT_HOME\webapps

检查Sample资源是否正常工作或

http://localhost:8080/{CONTEXT_PATH}/rest/cv

如果以上工作正常,请尝试上传资源方法。

P.S。我没有测试过你的上传代码,这是你的任务,你应该完成它。