PHP中cookies跨目录无法调用解决办法
2011-06-22 17:58
218 查看
来自 hideto
下载XFrie
首先,去http://xfire.codehaus.org下载最新版本的XFire
搭建webservice工程环境
在eclipse里创建一个叫webservice的java工程,然后依次添加src-service、src-conf、src-test和src-util这几个Source Folder以及web这个Folder
目录结构及文件如下:
代码
webservice
src-service
cn.hidetoishandsome.xfire.model
Book.java
cn.hidetoishandsome.xfire.service
BookService.java
cn.hidetoishandsome.xfire.service.impl
BookServiceImpl.java
src-conf
META-INF
xfire
services.xml
src-test
cn.hidetoishandsome.xfire.test
BookServiceTest.java
src-util
cn.hidetoishandsome.xfire.util
XfireClientFactory.java
web
WEB-INF
lib
web.xml
index.html
<script>render_code();</script>
然后将解压后的xfire的lib目录下所有jar包和xfire-all-1.*.jar复制到WEB-INF/lib目录
web.xml内容如下:
代码
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.4"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
<servlet-name>xfire</servlet-name>
<servlet-class>org.codehaus.xfire.transport.http.XFireConfigurableServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>xfire</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
</web-app>
<script>render_code();</script>
写一个BookService
我们将创建一个从ISBM号得到Book的Title的简单查询Web服务
首先创建Book.java
代码
package cn.hidetoishandsome.xfire.model;
public class Book {
private String title;
private String isbn;
public String getIsbn() {
return isbn;
}
public void setIsbn(String isbn) {
this.isbn = isbn;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
<script>render_code();</script>
然后写一个BookService接口BookService.java
代码
package cn.hidetoishandsome.xfire.service;
import cn.hidetoishandsome.xfire.model.Book;
public interface BookService {
Book findBookByISBN(String isbn);
}
<script>render_code();</script>
然后是BookService的实现BookServiceImpl.java
代码
package cn.hidetoishandsome.xfire.service.impl;
import cn.hidetoishandsome.xfire.model.Book;
import cn.hidetoishandsome.xfire.service.BookService;
public class BookServiceImpl implements BookService {
private Book book;
public BookServiceImpl() {
book = new Book();
book.setTitle("XFire Quick Start");
book.setIsbn("123456");
}
public Book findBookByISBN(String isbn) {
if (isbn.equals(book.getIsbn()))
return book;
throw new RuntimeException("Can't find book");
}
}
<script>render_code();</script>
在services.xml中配置要发布的服务
在src-conf的META-INF/xfire目录创建services.xml
代码
<beans xmlns="http://xfire.codehaus.org/config/1.0">
<service>
<name>BookService</name>
<namespace>http://localhost:8080/xfire/services/BookService</namespace>
<serviceClass>cn.hidetoishandsome.xfire.service.BookService</serviceClass>
<implementationClass>cn.hidetoishandsome.xfire.service.impl.BookServiceImpl</implementationClass>
</service>
</beans>
<script>render_code();</script>
其中name标签决定了我们创建的该服务的WSDL的URL为http://xx.xx.xx/xx/xx/BookService?wsdl
在Tomcat中发布
可以简单的修改Tomcat的server.xml来发布该Web服务,在<Host>标签中添加以下内容:
代码
Context path="/webservice" docBase="D:\project\webservice\web" reloadable="true"/>
<script>render_code();</script>
现在打开浏览器访问http://localhost:8080/webservice/services/BookService?wsdl来看看生成的WSDL文档
客户端调用测试
我们将使用一个XfireClientFactory.java工具类来帮我们调用该Web服务:
代码
package cn.hidetoishandsome.xfire.util;
import java.net.MalformedURLException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.codehaus.xfire.client.XFireProxyFactory;
import org.codehaus.xfire.service.Service;
import org.codehaus.xfire.service.binding.ObjectServiceFactory;
import org.springframework.util.Assert;
public class XfireClientFactory {
private static XFireProxyFactory serviceFactory = new XFireProxyFactory();
private static final Log log = LogFactory.getLog(XfireClientFactory.class);
private XfireClientFactory() {
}
public static <T> T getClient(String serviceURL, Class<T> serviceClass) {
Assert.notNull(serviceURL);
Assert.notNull(serviceClass);
Service serviceModel = new ObjectServiceFactory().create(serviceClass);
try {
return (T) serviceFactory.create(serviceModel, serviceURL);
} catch (MalformedURLException e) {
log.error(e.getMessage(), e);
return null;
}
}
}
<script>render_code();</script>
然后编写一个BookServiceTest.java来调用我们刚才发布的Web服务:
代码
package cn.hidetoishandsome.xfire.test;
import cn.hidetoishandsome.xfire.service.BookService;
import cn.hidetoishandsome.xfire.util.XfireClientFactory;
public class BookServieTest {
public static void main(String[] args) {
String serviceURL = "http://localhost:8080/webservice/services/BookService";
try {
BookService service = XfireClientFactory.getClient(serviceURL, BookService.class);
System.out.println("Book with ISBN '123456': 《" + service.findBookByISBN("123456").getTitle() + "》");
} catch (Exception e) {
e.printStackTrace();
}
}
}
<script>render_code();</script>
服务调用成功,Console打印内容如下:
代码
Book with ISBN '123456': 《XFire Quick Start》
<script>render_code();</script>
下载XFrie
首先,去http://xfire.codehaus.org下载最新版本的XFire
搭建webservice工程环境
在eclipse里创建一个叫webservice的java工程,然后依次添加src-service、src-conf、src-test和src-util这几个Source Folder以及web这个Folder
目录结构及文件如下:
代码
webservice
src-service
cn.hidetoishandsome.xfire.model
Book.java
cn.hidetoishandsome.xfire.service
BookService.java
cn.hidetoishandsome.xfire.service.impl
BookServiceImpl.java
src-conf
META-INF
xfire
services.xml
src-test
cn.hidetoishandsome.xfire.test
BookServiceTest.java
src-util
cn.hidetoishandsome.xfire.util
XfireClientFactory.java
web
WEB-INF
lib
web.xml
index.html
<script>render_code();</script>
然后将解压后的xfire的lib目录下所有jar包和xfire-all-1.*.jar复制到WEB-INF/lib目录
web.xml内容如下:
代码
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.4"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
<servlet-name>xfire</servlet-name>
<servlet-class>org.codehaus.xfire.transport.http.XFireConfigurableServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>xfire</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
</web-app>
<script>render_code();</script>
写一个BookService
我们将创建一个从ISBM号得到Book的Title的简单查询Web服务
首先创建Book.java
代码
package cn.hidetoishandsome.xfire.model;
public class Book {
private String title;
private String isbn;
public String getIsbn() {
return isbn;
}
public void setIsbn(String isbn) {
this.isbn = isbn;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
<script>render_code();</script>
然后写一个BookService接口BookService.java
代码
package cn.hidetoishandsome.xfire.service;
import cn.hidetoishandsome.xfire.model.Book;
public interface BookService {
Book findBookByISBN(String isbn);
}
<script>render_code();</script>
然后是BookService的实现BookServiceImpl.java
代码
package cn.hidetoishandsome.xfire.service.impl;
import cn.hidetoishandsome.xfire.model.Book;
import cn.hidetoishandsome.xfire.service.BookService;
public class BookServiceImpl implements BookService {
private Book book;
public BookServiceImpl() {
book = new Book();
book.setTitle("XFire Quick Start");
book.setIsbn("123456");
}
public Book findBookByISBN(String isbn) {
if (isbn.equals(book.getIsbn()))
return book;
throw new RuntimeException("Can't find book");
}
}
<script>render_code();</script>
在services.xml中配置要发布的服务
在src-conf的META-INF/xfire目录创建services.xml
代码
<beans xmlns="http://xfire.codehaus.org/config/1.0">
<service>
<name>BookService</name>
<namespace>http://localhost:8080/xfire/services/BookService</namespace>
<serviceClass>cn.hidetoishandsome.xfire.service.BookService</serviceClass>
<implementationClass>cn.hidetoishandsome.xfire.service.impl.BookServiceImpl</implementationClass>
</service>
</beans>
<script>render_code();</script>
其中name标签决定了我们创建的该服务的WSDL的URL为http://xx.xx.xx/xx/xx/BookService?wsdl
在Tomcat中发布
可以简单的修改Tomcat的server.xml来发布该Web服务,在<Host>标签中添加以下内容:
代码
Context path="/webservice" docBase="D:\project\webservice\web" reloadable="true"/>
<script>render_code();</script>
现在打开浏览器访问http://localhost:8080/webservice/services/BookService?wsdl来看看生成的WSDL文档
客户端调用测试
我们将使用一个XfireClientFactory.java工具类来帮我们调用该Web服务:
代码
package cn.hidetoishandsome.xfire.util;
import java.net.MalformedURLException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.codehaus.xfire.client.XFireProxyFactory;
import org.codehaus.xfire.service.Service;
import org.codehaus.xfire.service.binding.ObjectServiceFactory;
import org.springframework.util.Assert;
public class XfireClientFactory {
private static XFireProxyFactory serviceFactory = new XFireProxyFactory();
private static final Log log = LogFactory.getLog(XfireClientFactory.class);
private XfireClientFactory() {
}
public static <T> T getClient(String serviceURL, Class<T> serviceClass) {
Assert.notNull(serviceURL);
Assert.notNull(serviceClass);
Service serviceModel = new ObjectServiceFactory().create(serviceClass);
try {
return (T) serviceFactory.create(serviceModel, serviceURL);
} catch (MalformedURLException e) {
log.error(e.getMessage(), e);
return null;
}
}
}
<script>render_code();</script>
然后编写一个BookServiceTest.java来调用我们刚才发布的Web服务:
代码
package cn.hidetoishandsome.xfire.test;
import cn.hidetoishandsome.xfire.service.BookService;
import cn.hidetoishandsome.xfire.util.XfireClientFactory;
public class BookServieTest {
public static void main(String[] args) {
String serviceURL = "http://localhost:8080/webservice/services/BookService";
try {
BookService service = XfireClientFactory.getClient(serviceURL, BookService.class);
System.out.println("Book with ISBN '123456': 《" + service.findBookByISBN("123456").getTitle() + "》");
} catch (Exception e) {
e.printStackTrace();
}
}
}
<script>render_code();</script>
服务调用成功,Console打印内容如下:
代码
Book with ISBN '123456': 《XFire Quick Start》
<script>render_code();</script>
相关文章推荐
- PHP中cookies跨目录无法调用解决办法
- PHP调用gradle在服务器自动生成apk出现的lint无法创建目录解决
- 文件或目录损坏且无法读取的解决办法
- 无法加载php_curl.dll解决办法
- php上传文件后无法移动到指定目录的解决
- php 无法加载mcrypt.dll的解决办法
- PHP技巧:解决PHP相对目录问题最有效的办法
- 【没有assembly目录】spark2.0.0启动时无法访问spark-assembly-*.jar的解决办法
- Xcode import文件时无法找到同级其他目录的文件解决办法
- 组件设置window2008 64位系统无法调用Microsoft.Office.Interop组件进行文件另存的解决办法
- PHP微信公众号服务端提示Notice: Undefined index: HTTP_RAW_POST_DATA in xxxx,且无法正常返回的解决办法
- 解决kafka数据目录迁移后,flume无法正常调用Consumer的问题
- 解决kafka数据目录迁移后,flume无法正常调用Consumer的问题
- android 调用javascript 无法弹出Alert的解决办法
- php无法保存cookies问题解决
- WordPress自动升级过程中显示 无法定位WordPress内容目录(wp-content)解决办法
- Eclipse下无法自动编译,或者WEB-INF/classes目录下没文件,编译失败的解决办法
- 文件或目录损坏且无法读取的解决办法
- 无法创建目录或文件问题的解决办法
- Android SDK Manager在添加Platforms和其他Packages时提示无法在相应目录创建文件夹的解决办法!