简体   繁体   中英

Getting 404 error on running Restful web Service in java

I am trying to create a restful web service in java. Whenever I am trying to run it I am getting 404 error and I am not able to figure out why!

I am referring JavaTPoint for this.

This is how my project looks like-

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">  
     <servlet>  
        <servlet-name>Jersey REST 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.javatpoint.rest</param-value>  
        </init-param>  
        <load-on-startup>1</load-on-startup>  
      </servlet>  
      <servlet-mapping>  
        <servlet-name>Jersey REST Service</servlet-name>  
        <url-pattern>/rest/*</url-pattern>  
      </servlet-mapping>  
    </web-app> 

FileDownloadService.java

package com.javatpoint.rest;

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

@Path("/files")
public class FileDownloadService {
    @GET
    @Path("/txt")
    @Produces(MediaType.TEXT_XML)
    public String getFile() {
        String response = null;
        response = "<?xml version='1.0'?><hello>Hello xml</hello>";
        return response;
    }
}

index.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
    <a href="rest/files/txt">Download Text File</a>
</body>
</html>

I am getting following response-

在此处输入图片说明

Can anyone point out what I am doing wrong here?Any help will be appreciated..

您需要指定api的完整路径

<a href="[full path to your servlet container]/rest/files/txt">Download Text File</a>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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