我为我的应用程序创建了一个webservice,用于跟踪用户详细信息, 其余的webservice是使用jersy api创建的。网络服务运行良好, 但是当我从客户端应用程序拨打电话时它没有找到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());
}
}
}
以下是我在打印后收到的错误:
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的几篇帖子中也阅读了文档,但没有奏效。 任何人都可以帮我解决这个问题。
答案 0 :(得分:0)
这可以在浏览器中使用吗?http://localhost:8080/my_webservice/user/UserLogin/validUser/ 或没有尾随/ http://localhost:8080/my_webservice/user/UserLogin/validUser
答案 1 :(得分:0)
我终于找到了解决这个问题的方法,我重新安装了我的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>";
}
}
我也改变了我的客户端代码,如下所示 休息服务客户:
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());
}
}
它对我有用。感谢大家的帮助。