繁体   English   中英

从客户端呼叫时未找到剩余Web服务

[英]Rest web service not found when calling from client

我已经为我的应用程序创建了一个Web服务,用于跟踪用户详细信息,其余的Web服务是使用jersy api创建的。 Web服务运行正常,但是当我从客户端应用程序拨打电话时,找不到Web服务,但是,如果我在浏览器中键入相同的URL,则会为我提供正确的输出。 以下是服务代码:

package com.user.login;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import org.codehaus.jettison.json.JSONException;
@Path("/UserLogin")
public class UserLogin {

    private String userName;
    private String password;
    private boolean rememberMe;

    public UserLogin(final String userName, final String password,
            final boolean rememberMe, final String country, final String city,final String ipAddress) {
        this.userName = userName;
        this.password = password;
        this.rememberMe = rememberMe;
    }

    @GET
    @Path("validUser")
    @Produces(MediaType.TEXT_PLAIN)
    public String validUserLogin() throws JSONException {
        if ((this.userName == null) || (this.password == null)) {
            return "<p>Hello</p>";
        } 
        return "<p>Hi</p>";
    }
}

下面是部署描述符:

<?xml version="1.0" encoding="UTF-8"?>
<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>
        <display-name>JAX-RS REST Servlet</display-name>
        <servlet-name>JAX-RS REST Servlet</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.user.login</param-value>
</init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>JAX-RS REST Servlet</servlet-name>
        <url-pattern>/user/*</url-pattern>
    </servlet-mapping>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
</web-app>

我首先在tomcat apache上启动该服务,然后在同一服务器上启动我的客户端应用程序后对其进行测试,以测试它是否正在运行。 客户代码如下:

package com.src.main.service;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

public class UserService {

    public static final String BASE_URI = "http://localhost:8080/my_webservice/user";
    public static final String PATH_VALID_USER = "/UserLogin/validUser/";

    public UserService(){
        try{
            HttpClient client = new DefaultHttpClient();
            HttpGet request = new HttpGet(BASE_URI+PATH_VALID_USER);
            HttpResponse response = client.execute(request);
            System.err.println("content type : \n"+response.getEntity().getContentType()+" \ncontent: \n"+response.getEntity().getContent());
            BufferedReader buffer = new BufferedReader(new  InputStreamReader(response.getEntity().getContent()));
            String line="";
            while(( line= buffer.readLine()) != null){
            System.err.println(line);
        }
        }catch(ClientProtocolException exception){
            System.err.println("Client Exception: \n"+exception.getStackTrace());
        }catch(IOException ioException){
            System.err.println("ioException :\n"+ioException.getStackTrace());
        }
    }
}

以下是在打印后iam收到的错误:

content type : 
Content-Type: text/html; charset=WINDOWS-1252 
content: 
org.apache.http.conn.EofSensorInputStream@1677737
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<HTML><HEAD>
<TITLE>404 Not found</TITLE>
</HEAD><BODY><H1>Not found</H1>
The requested URL /my_webservice/user/UserLogin/validUser/ was not found on this server</BODY></HTML>

我已经提到过Stackoveflow上有关该主题的上一篇文章,但不了解我所缺少的地方。 有什么不同的部署方式,我也阅读了Stackoverflow的几篇文章中给出的文档,但是没有用。 谁能帮我解决这个问题。

我终于找到了解决这个问题的方法,我重新安装了tomcat服务器,并对服务和客户端代码进行了微小的更改,如下所示进行测试。 服务代码:

package com.user.login;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import org.codehaus.jettison.json.JSONException;
@Path("/UserLogin")
public class UserLogin {
@GET
@Produces(MediaType.TEXT_PLAIN)
public String respondAsReady() {
   return "Web service is ready!";
}
private String userName;
private String password;
private boolean rememberMe;
public UserLogin(){
}
@GET
@Path("/validUser")
@Produces(MediaType.TEXT_PLAIN)
public String validUserLogin() throws JSONException {
if ((this.userName == null) || (this.password == null)) {
        return "<p>Hello</p>";
    } 
return "<p>Hi</p>";
   }
}

我也更改了我的客户端代码,如下所示的Rest Service客户端:

package com.src.main.service;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.params.ClientPNames;
import org.apache.http.client.params.CookiePolicy;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;
import org.apache.http.util.EntityUtils;
public class UserService {
public static final String BASE_URI = "http://localhost:8088/my_webservice/user";
public static final String PATH_VALID_USER = "/UserLogin/validUser/";
public UserService(){
   try{
   HttpContext  context = new BasicHttpContext();
   HttpClient client = new DefaultHttpClient();
   client.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.RFC_2109);  
   URI uri=new URIBuilder(BASE_URI+PATH_VALID_USER).build();    
   HttpGet request = new HttpGet(uri);  
   HttpResponse response = client.execute(request,context); 
   System.err.println("content type : \n"+EntityUtils.toString(response.getEntity()));  
   }catch(ClientProtocolException exception){
   System.err.println("Client Exception: \n"+exception.getStackTrace());    
   }catch(IOException ioException){
     System.err.println("ioException :\n"+ioException.getStackTrace());
   }catch(URISyntaxException exception){
      System.err.println("URI Syntax exxceptin :"+exception.getStackTrace());
   }        
}

它为我工作。感谢大家的帮助。

暂无
暂无

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

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