如何在jersey api中修复404未找到的错误

时间:2015-04-29 18:57:34

标签: java eclipse rest jersey

我使用jersey api创建了一个Web服务,但我遇到了一个问题。 Hello.java是我的Web服务文件。 ClientTest.java是我使用该服务的客户端文件,而web.xml是deployement descripter。 我在eclipse中添加了所有的jersey jar文件

Hello.java如下: - `             包装asdlmkmax;

import javax.ws.rs.GET;  
import javax.ws.rs.Path;  
import javax.ws.rs.Produces;  
import javax.ws.rs.core.MediaType;  
@Path("/hello")  
public class Hello 
{
    // This method is called if HTML and XML is not requested  
      @GET  
      @Produces(MediaType.TEXT_PLAIN)  
      public String sayPlainTextHello() 
      {  
          return "Hello Jersey Plain";  
      }  
      // This method is called if XML is requested  
      @GET  
      @Produces(MediaType.TEXT_XML)  
      public String sayXMLHello() 
      {  
          return "<?xml version=\"1.0\"?>" + "<hello> Hello Jersey" + "</hello>";  
      }  

      // This method is called if HTML is requested  
      @GET  
      @Produces(MediaType.TEXT_HTML)  
      public String sayHtmlHello() 
      {  
          return "<html> " + "<title>" + "Hello Jersey" + "</title>"  
            + "<body><h1>" + "Hello Jersey HTML" + "</h1></body>" + "</html> ";  
      }  

}

ClientTest.java如下: -     包sericeconsumer;

import java.net.URI;  
import javax.ws.rs.client.Client;  
import javax.ws.rs.client.ClientBuilder;  
import javax.ws.rs.client.WebTarget;  
import javax.ws.rs.core.MediaType;  
import javax.ws.rs.core.UriBuilder;  
import org.glassfish.jersey.client.ClientConfig;  
public class ClientTest {static WebTarget target;
      public static void main(String[] args) {  
            ClientConfig config = new ClientConfig();  
            Client client = ClientBuilder.newClient(config);  
            WebTarget target = ClientBuilder.newClient().target("http://localhost:8080/asdlmkmax/rest/");

            //Now printing the server code of different media type  
            System.out.println(target.path("hello").request().accept(MediaType.TEXT_PLAIN).get(String.class));  
            /*System.out.println(target.path("rest").path("hello").request().accept(MediaType.TEXT_XML).get(String.class));  
            System.out.println(target.path("rest").path("hello").request().accept(MediaType.TEXT_HTML).get(String.class));*/  
          }  
          private static URI getBaseURI() {  
            //here server is running on 4444 port number and project name is restfuljersey  
            return UriBuilder.fromUri("http://localhost:8080/asdlmkmax/").build();  
          }  

}

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>asdlmkmax</display-name>

 <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>  
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
</web-app>

index.html如下: -

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<a href="rest/hello">Click Here</a>  
</body>
</html>

文件ClientTest.java是不同的java项目。其他动态web项目是&#34; asdlmkmax&#34; 错误: -

Exception in thread "main" javax.ws.rs.NotFoundException: HTTP 404 Not Found
at       org.glassfish.jersey.client.JerseyInvocation.convertToException(JerseyInvocation.java:956)
at org.glassfish.jersey.client.JerseyInvocation.translate(JerseyInvocation.java:795)
at org.glassfish.jersey.client.JerseyInvocation.access$500(JerseyInvocation.java:91)
at org.glassfish.jersey.client.JerseyInvocation$2.call(JerseyInvocation.java:683)
at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
at org.glassfish.jersey.internal.Errors.process(Errors.java:228)
at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:424)
at org.glassfish.jersey.client.JerseyInvocation.invoke(JerseyInvocation.java:679)
at org.glassfish.jersey.client.JerseyInvocation$Builder.method(JerseyInvocation.java:408)
at org.glassfish.jersey.client.JerseyInvocation$Builder.get(JerseyInvocation.java:308)
at sericeconsumer.ClientTest.main(ClientTest.java:19)

0 个答案:

没有答案