从客户端调用时找不到 Rest Web 服务
Rest web service not found when calling from client
我已经为我的应用程序创建了一个网络服务,用于跟踪用户详细信息,
其余的网络服务是使用 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 之前关于这个主题的 post,但不明白我遗漏了哪里。是否有任何不同的部署方式,我阅读了 Whosebug 的一些 post 中给出的文档,但没有奏效。
谁能帮我解决这个问题。
这在浏览器中有效吗?http://localhost:8080/my_webservice/user/UserLogin/validUser/
或者没有尾随 /
http://localhost:8080/my_webservice/user/UserLogin/validUser
我终于找到了这个问题的解决方案,我重新安装了我的 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());
}
}
它对 me.Thanks 每个人都有帮助。
我已经为我的应用程序创建了一个网络服务,用于跟踪用户详细信息, 其余的网络服务是使用 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 之前关于这个主题的 post,但不明白我遗漏了哪里。是否有任何不同的部署方式,我阅读了 Whosebug 的一些 post 中给出的文档,但没有奏效。 谁能帮我解决这个问题。
这在浏览器中有效吗?http://localhost:8080/my_webservice/user/UserLogin/validUser/ 或者没有尾随 / http://localhost:8080/my_webservice/user/UserLogin/validUser
我终于找到了这个问题的解决方案,我重新安装了我的 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());
}
}
它对 me.Thanks 每个人都有帮助。