webservice如今开发运用已经很容易了,这都得益于巨头们的努力.众多框架:cxf……
这个示例也是用的cxf,这是我的第一个webservice 例子,jar包这里没有上传,可以到官网下载。
HelloService.java代码:
package org.iteye.bbjava.ws.service;
import javax.jws.WebService;
@WebService
public interface HelloService {
String sayHi(String text);
}
HelloServcieImpl.java代码:
package org.iteye.bbjava.ws.service.impl;
import javax.jws.WebService;
import org.iteye.bbjava.ws.service.HelloService;
@WebService(endpointInterface = "org.iteye.bbjava.ws.service.HelloService")
public class HelloServiceImpl implements HelloService {
public String sayHi(String text) {
System.out.println("sayHi called");
return "Hello " + text;
}
}
applicationContext.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
<import resource="classpath:META-INF/cxf/cxf.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
<jaxws:endpoint id="helloService" implementor="#hello"
address="/HelloServiceImpl" />
<bean id="hello" class="org.iteye.bbjava.ws.service.impl.HelloServiceImpl" />
</beans>
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>ws-spring</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<servlet>
<servlet-name>CXFServlet</servlet-name>
<servlet-class>
org.apache.cxf.transport.servlet.CXFServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
配置的说明:
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/*</url-pattern>
上面的url-pattern的作用比较重要,因为CXFServlet会“过虑”关相请求。我们就以上面的代码为例,在浏览器中输入
http://localhost:8080/ 会进入webservice相关页面,也就是说在正常的main.jsp它进不去了。
那如何解决呢?
请看:web.xml:
引用
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/service/ *</url-pattern>
HelloServiceImpl.java:
@WebService(endpointInterface = "org.iteye.bbjava.ws.service.HelloService",targetNamespace = "http://localhost:8080/service")
到此服务端的内容就结束了。
下面是
客户端了,
package org.iteye.bbjava.ws;
import org.iteye.bbjava.ws.service.HelloService;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class ClientTest {
private ClientTest() {
}
public static void main(String args[]) throws Exception {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
new String[] { "applicationContext.xml" });
HelloService client = (HelloService) context.getBean("helloService");
String response = client.sayHi("zhang");
System.out.println("Response: " + response);
}
}
客户端的:
applicationContext.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schema/jaxws.xsd">
<bean id="helloService" class="org.iteye.bbjava.ws.service.HelloService"
factory-bean="clientFactory" factory-method="create" />
<bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
<property name="serviceClass"
value="org.iteye.bbjava.ws.service.HelloService" />
<property name="address" value="http://localhost:8080/ws-spring/HelloServiceImpl" />
</bean>
</beans>
配置的说明:
引用
在client.xml:
<property name="address" value="http://localhost:8080/ws-spring/HelloServiceImpl" />
改为
<property name="address" value="http://localhost:8080/ws-spring/service/HelloServiceImpl" />
服务端的applicationContext.xml 细则说明:
引用
客户端的applicationContext.xml细则说明:
引用
<bean id="helloService" class="org.iteye.bbjava.ws.service.HelloService"
factory-bean="clientFactory" factory-method="create" />
<bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
<property name="serviceClass"
value="org.iteye.bbjava.ws.service.HelloService" />
<property name="address" value="http://localhost:8080/ws-spring/HelloServiceImpl" />
</bean>
上面两行。
两个bean共同组成了一个webserive的接口客户端的核心,其中
<property name="address" value="http://localhost:8080/ws-spring/HelloServiceImpl" />
指出了,服务端的地址。(文字不好组织)
分享到:
相关推荐
webservice-CXF-spring 实现server+client
CXF是apache旗下的开源框架,是一套非常流行的web service框架,同时它能与spring进行完美结合。
cxf与spring集成 需要最少的jar如下: cxf-2.3.3.jar geronimo-annotation_1.0_spec-1.1.1.jar geronimo-jaxws_2.2_spec-1.0.jar geronimo-stax-api_1.0_spec-1.0.1.jar geronimo-ws-metadata_2.0_spec-1.1.3.jar ...
webservice cxf spring jar包大全,下载,超好用的
apache-cxf-3.1.6官方版,其中有spring包。2016-4-14最新版。
cxf 与 Spring 结合,此例子中包含服务端和客户端, 2个工程都是与SSH 结合使用。相信对刚接触的朋友有用!
neethi-3.0.2.jar,spring-asm-3.0.7.RELEASE.jar,spring-asm-3.0.7.RELEASE.jar,spring-beans-3.0.7.RELEASE.jar,spring-context-3.0.7.RELEASE.jar,spring-core-3.0.7.RELEASE.jar,spring-expression-3.0.7....
spring 整合CXF 客户端demo,包含相关jar包
spring 整合cxf 服务端demo,包含相关jar包
由Spring Boot和Apache CXF支持的企业和生产就绪的SOAP Web服务 功能包括: 使用WSDL / XSD中的JAX-B生成所有必要的Java类(使用补充的Maven插件 使用100%纯Java配置在Spring Context中启动Apache CXF 端点初始...
cxf-spring-cloud-netflix-docker Spring Boot和Spring Cloud兼容性: : 将Spring Boot应用程序与Spring Cloud Netflix和Docker结合在一起的示例项目它是作为此博客文章的展示创建的: 并且被该Ansible存储库使用:...
apache-cxf-3.0.13官方完整jar包,包含bin目录和api文档,还有完整的simple来参考。可与spring3.2.0完美整合。
Spring4.3整合CXF3.0.4需要使用的Jar包,Spring4.0以上需要使用CXF3.0以上的jar包才可以,之前使用CXF2.7会报各种错误,cxf-core-3.0.4.jar,cxf-rt-bindings-soap-3.0.4.jar
Apache CXF = Celtix + XFire,开始叫 Apache CeltiXfire,后来更名为 Apache CXF 了,以下简称为 CXF。CXF 继承了 Celtix 和 XFire 两大开源项目的精华,提供了对 JAX-WS 全面的支持,并且提供了多种 Binding 、...
cxf-spring-webapp 使用Apache CXF和Spring的示例Web应用程序。
spring-expression-3.0.7.RELEASE.jar spring-jms-3.0.7.RELEASE.jar spring-tx-3.0.7.RELEASE.jar spring-web-3.0.7.RELEASE.jar stax2-api-3.1.1.jar velocity-1.7.jar woodstox-core-asl-4.1.4.jar wsdl4j-1.6.2....
cxf+spring开发所必备的jar包压缩包
依赖的包: commons-logging-1.1.jar geronimo-activation_1.1_spec-1.0-M1.jar (or Sun's Activation jar) geronimo-annotation_1.0...spring-context-2.0.4.jar spring-web-2.0.4.jar CXF jar包: cxf-2.1.jar
spring 4.2.0 集成的cxf3.1.18的jar包,cxf-core-3.1.18.jar、cxf-rt-bindings-soap-3.1.18.jar、cxf-rt-databinding-jaxb-3.1.18.jar、cxf-rt-frontend-jaxws-3.1.18.jar、cxf-rt-frontend-simple-3.1.18.jar、cxf-...
apache-cxf-2.7.10,java webservices开发相关jar包,与Spring集成