在java

时间:2017-05-17 08:05:17

标签: java web-services rest

我正在尝试在java中创建一个安静的Web服务。每当我试图运行它时,我收到404错误,我无法找出原因!

我指的是JavaTPoint

这就是我的项目的样子 -

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>

我收到了以下回复 -

enter image description here

任何人都可以指出我在这里做错了吗?任何帮助都将受到赞赏..

1 个答案:

答案 0 :(得分:1)

您需要指定api的完整路径

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