xfire集成spring开发webservice怎么配置发布地址
2个回答
展开全部
1.首先建一个Web工程,引入相应的jar包,Xfire开发最精简jar包下载
服务器端:
commons-logging-1.1.1.jar
jdom-1.0.jar
org.springframework.aop-3.1.1.RELEASE.jar
org.springframework.asm-3.1.1.RELEASE.jar
org.springframework.beans-3.1.1.RELEASE.jar
org.springframework.context-3.1.1.RELEASE.jar
org.springframework.core-3.1.1.RELEASE.jar
org.springframework.expression-3.1.1.RELEASE.jar
org.springframework.web.servlet-3.1.1.RELEASE.jar
org.springframework.web-3.1.1.RELEASE.jar
wsdl4j-1.6.1.jar
xfire-all-1.2.6.jar
客户端:
com.springsource.org.junit-4.7.0.jar
commons-codec-1.3.jar
commons-httpclient-3.0.jar
commons-logging-1.1.1.jar
jdom-1.0.jar
wsdl4j-1.6.1.jar
xfire-all-1.2.6.jar
XmlSchema-1.1.jar
2.修改web.xml,加入以下代码:
[html] view plain copy
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>xfireServlet</servlet-name>
<servlet-class>org.codehaus.xfire.spring.XFireSpringServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>xfireServlet</servlet-name>
<url-pattern>/service/*</url-pattern>
</servlet-mapping>
3.在classpath下加入Spring配置文件applicationContext.xml,加入以下代码:
[html] view plain copy
<context:component-scan base-package="my.webservice" />
<!-- XFire start -->
<import resource="classpath:org/codehaus/xfire/spring/xfire.xml" />
<bean id="webAnnotations" class="org.codehaus.xfire.annotations.jsr181.Jsr181WebAnnotations" />
<bean id="jsr181HandlerMapping" class="org.codehaus.xfire.spring.remoting.Jsr181HandlerMapping">
<property name="xfire" ref="xfire" />
<property name="webAnnotations" ref="webAnnotations" />
</bean>
<!-- XFire end -->
4.定义WebService接口,添加相应注解:
[java] view plain copy
@WebService
public interface IBookService {
@WebMethod
public Book getBook();
}
5.接口实现类,注解中serviceName定义发布的服务名,endpointInterface定义实现的接口:
[java] view plain copy
@Component
@WebService(serviceName="BookService",
endpointInterface = "my.webservice.IBookService")
public class BookServiceImpl implements IBookService {
@Override
public Book getBook() {
Book b = new Book(1, "Java核心思想", 100);
System.out.println(">>>>>>Server: " + b);
return b;
}
}
6.以上便是服务端的配置及实现,是不是很简单。把工程发布到Tomcat并启动,在浏览器中输入:http://127.0.0.1:8080/XFireTest/service/BookService?wsdl(BookService为上面serviceName定义的名称),如果浏览器中显示BookService相关xml信息,则表示WebService发布成功。
7.客户端调用服务端方法:
(1)使用接口调用,此方法需要发布服务者提供接口,或我们自己通过wsdl生成接口。自己生成接口方法:
wsimport -keep -p my.client http://127.0.0.1:8080/XFireTest/service/BookService?wsdl
-keep 指示保留生成的文件,-p 指定需要在其中生成构件的包名称。http://127.0.0.1:8080/XFireTest/service/BookService?wsdl 是WSDL文件的位置。
wsimport在JAVA_HOME/bin目录下,应该已加到path环境变量中,可直接在cmd中运行以上命令,注意先进入要生成类的目录(如src)再执行。
[java] view plain copy
@Test
public void testBookService() {
Service serviceModel = new ObjectServiceFactory().create(IBookService.class);
String url = "http://127.0.0.1:8080/XFireTest/service/BookService";
IBookService service = null;
try {
service = (IBookService) new XFireProxyFactory().create(serviceModel, url);
Book b = service.getBook();
System.out.println(">>>>>>>>Client: " + b);
} catch (Exception e) {
e.printStackTrace();
}
}
(2)通过wsdl调用,该方法如果返回值是String可正常使用,如果像本列中返回Book类型,则打印值为[#document: null],返回类型是org.apache.xerces.dom.DocumentImpl,需手动解析。我在项目中使用时,发布服务的时候把数据用xml形式返回,客户端用dom4j解析。
[java] view plain copy
@Test
public void wsdlTest() throws MalformedURLException, Exception {
Client client = new Client(new URL("http://127.0.0.1:8080/XFireTest/service/BookService?wsdl"));
Object[] results = client.invoke("getBook", new Object[] {});
System.out.println(results[0]);
}
服务器端:
commons-logging-1.1.1.jar
jdom-1.0.jar
org.springframework.aop-3.1.1.RELEASE.jar
org.springframework.asm-3.1.1.RELEASE.jar
org.springframework.beans-3.1.1.RELEASE.jar
org.springframework.context-3.1.1.RELEASE.jar
org.springframework.core-3.1.1.RELEASE.jar
org.springframework.expression-3.1.1.RELEASE.jar
org.springframework.web.servlet-3.1.1.RELEASE.jar
org.springframework.web-3.1.1.RELEASE.jar
wsdl4j-1.6.1.jar
xfire-all-1.2.6.jar
客户端:
com.springsource.org.junit-4.7.0.jar
commons-codec-1.3.jar
commons-httpclient-3.0.jar
commons-logging-1.1.1.jar
jdom-1.0.jar
wsdl4j-1.6.1.jar
xfire-all-1.2.6.jar
XmlSchema-1.1.jar
2.修改web.xml,加入以下代码:
[html] view plain copy
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>xfireServlet</servlet-name>
<servlet-class>org.codehaus.xfire.spring.XFireSpringServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>xfireServlet</servlet-name>
<url-pattern>/service/*</url-pattern>
</servlet-mapping>
3.在classpath下加入Spring配置文件applicationContext.xml,加入以下代码:
[html] view plain copy
<context:component-scan base-package="my.webservice" />
<!-- XFire start -->
<import resource="classpath:org/codehaus/xfire/spring/xfire.xml" />
<bean id="webAnnotations" class="org.codehaus.xfire.annotations.jsr181.Jsr181WebAnnotations" />
<bean id="jsr181HandlerMapping" class="org.codehaus.xfire.spring.remoting.Jsr181HandlerMapping">
<property name="xfire" ref="xfire" />
<property name="webAnnotations" ref="webAnnotations" />
</bean>
<!-- XFire end -->
4.定义WebService接口,添加相应注解:
[java] view plain copy
@WebService
public interface IBookService {
@WebMethod
public Book getBook();
}
5.接口实现类,注解中serviceName定义发布的服务名,endpointInterface定义实现的接口:
[java] view plain copy
@Component
@WebService(serviceName="BookService",
endpointInterface = "my.webservice.IBookService")
public class BookServiceImpl implements IBookService {
@Override
public Book getBook() {
Book b = new Book(1, "Java核心思想", 100);
System.out.println(">>>>>>Server: " + b);
return b;
}
}
6.以上便是服务端的配置及实现,是不是很简单。把工程发布到Tomcat并启动,在浏览器中输入:http://127.0.0.1:8080/XFireTest/service/BookService?wsdl(BookService为上面serviceName定义的名称),如果浏览器中显示BookService相关xml信息,则表示WebService发布成功。
7.客户端调用服务端方法:
(1)使用接口调用,此方法需要发布服务者提供接口,或我们自己通过wsdl生成接口。自己生成接口方法:
wsimport -keep -p my.client http://127.0.0.1:8080/XFireTest/service/BookService?wsdl
-keep 指示保留生成的文件,-p 指定需要在其中生成构件的包名称。http://127.0.0.1:8080/XFireTest/service/BookService?wsdl 是WSDL文件的位置。
wsimport在JAVA_HOME/bin目录下,应该已加到path环境变量中,可直接在cmd中运行以上命令,注意先进入要生成类的目录(如src)再执行。
[java] view plain copy
@Test
public void testBookService() {
Service serviceModel = new ObjectServiceFactory().create(IBookService.class);
String url = "http://127.0.0.1:8080/XFireTest/service/BookService";
IBookService service = null;
try {
service = (IBookService) new XFireProxyFactory().create(serviceModel, url);
Book b = service.getBook();
System.out.println(">>>>>>>>Client: " + b);
} catch (Exception e) {
e.printStackTrace();
}
}
(2)通过wsdl调用,该方法如果返回值是String可正常使用,如果像本列中返回Book类型,则打印值为[#document: null],返回类型是org.apache.xerces.dom.DocumentImpl,需手动解析。我在项目中使用时,发布服务的时候把数据用xml形式返回,客户端用dom4j解析。
[java] view plain copy
@Test
public void wsdlTest() throws MalformedURLException, Exception {
Client client = new Client(new URL("http://127.0.0.1:8080/XFireTest/service/BookService?wsdl"));
Object[] results = client.invoke("getBook", new Object[] {});
System.out.println(results[0]);
}
展开全部
给你个简单的例子吧
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="
xmlns:xsi="
xmlns:jaxws="
xsi:schemaLocation="
<import resource="classpath:META-INF/cxf/cxf.xml"/>
<import resource="classpath:META-INF/cxf/cxf-servlet.xml"/>
<!-- jaxws:server示例 首先在web.xml中添加servlet
<servlet>
<servlet-name>CXFService</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>CXFService</servlet-name>
<url-pattern>/deviceWService/*</url-pattern>
</servlet-mapping>
<jaxws:server id="androidWService" serviceClass="webservice注解的服务类" address="/访问路径">
<jaxws:serviceBean/>
</jaxws:server>
-->
<jaxws:server id="androidWService" serviceClass="com.my.project.plugin.webservice.MyWebservice" address="/ddd">
</jaxws:server>
</beans>
@WebService
public class MyWebservice {
public String ddd() {
return "MyWebservice";
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询