设为首页 收藏本站
查看: 852|回复: 0

[经验分享] Apache CXF 2.0 基于spring的web service开发

[复制链接]

尚未签到

发表于 2017-1-11 10:31:07 | 显示全部楼层 |阅读模式
Apache CXF Developing a Service using JAX-WS
two approaches:
I.WSDL First Development
Generate starting point code.
Implement the service's operations.
Publish the implemented service.
II.Java First Development

Create a Service Endpoint Interface (SEI) that defines the methods you wish to expose as a service.
Add the required annotations to your code.
Generate the WSDL contract for your service.
Publish the service.
注意事项:
JAX-WS relies on the annotation feature of Java 5. 通过对Interface和implement class添加annotation来映射wsdl.
使用Java First Development,首先要创建SEI和实现类。
然后添加annotation
@WebService
name:用于Interface,属映射到wsdl:portType element的name属性。
targetNamespace:用于Interface和implement,如果不指定,缺省会使用包名做为wsdl名空间。
serviceName:用于implement,表示wsdl服务名。
portName:用于implement,表示wsdl:port 的name属性。
endpointInterface:用于implement,指定Interface全名,包括包名。
如果Service Endpoint Interface中包含用户自定义的复杂类型,如Address,Role etc.
@SOAPBinding如果用document style(缺省),parameterStyle要使用ParameterStyle.BARW(缺省是ParameterStyle.WRAPPED ),如果用RPC style,则只能使用ParameterStyle.WRAPPED。
@WebMethod(exclude=true)可以将不希望暴露的方法屏蔽。
对于简单的web service,其他annotation可以省略不用。

Property  Values  Description  
style  Style.DOCUMENT (default)
Style.RPC  Specifies the style of the SOAP message. If RPC style is specified, each message part within the SOAP body is a parameter or return value and will appear inside a wrapper element within the soap:body element. The message parts within the wrapper element correspond to operation parameters and must appear in the same order as the parameters in the operation. If DOCUMENT style is specified, the contents of the SOAP body must be a valid XML document, but its form is not as tightly constrained.  
use  Use.LITERAL (default)
Use.ENCODED  Specifies how the data of the SOAP message is streamed.  
parameterStyle  ParameterStyle.BARE
ParameterStyle.WRAPPED (default)  Specifies how the method parameters, which correspond to message parts in a WSDL contract, are placed into the SOAP message body. A parameter style of BARE means that each parameter is placed into the message body as a child element of the message root. A parameter style of WRAPPED means that all of the input parameters are wrapped into a single element on a request message and that all of the output parameters are wrapped into a single element in the response message. If you set the style to RPC you must use the WRAPPED parameter style.  


This example will lead you through creating your first service with Spring. You'll learn how to:

Set up your build for CXF
Writing a simple JAX-WS service
Set up the HTTP transport
This example corresponds to the spring_http example in the CXF distribution.

Setting up your build
Open up your favorite IDE and create a new project. The first thing we need to do is add the necessary CXF dependencies to the project. You can find these dependencies in the CXF distribution in the lib directory.

commons-logging-1.1.jar
geronimo-activation_1.1_spec-1.0-M1.jar (or Sun's Activation jar)
geronimo-annotation_1.0_spec-1.1.jar (JSR 250)
geronimo-javamail_1.4_spec-1.0-M1.jar (or Sun's JavaMail jar)
geronimo-servlet_2.5_spec-1.1-M1.jar (or Sun's Servlet jar)
geronimo-ws-metadata_2.0_spec-1.1.1.jar (JSR 181)
jaxb-api-2.0.jar
jaxb-impl-2.0.5.jar
jaxws-api-2.0.jar
saaj-api-1.3.jar
saaj-impl-1.3.jar
stax-api-1.0.1.jar
wsdl4j-1.6.1.jar
wstx-asl-3.2.1.jar
XmlSchema-1.2.jarThe Spring jars:

aopalliance-1.0.jar
spring-core-2.0.4.jar
sprint-beans-2.0.4.jar
spring-context-2.0.4.jar
spring-web-2.0.4.jarAnd the CXF jar:

cxf-2.0-incubator.jarWriting your Service
First we'll write our service interface. It will have one operation called "sayHello" which says "Hello" to whoever submits their name.

package demo.spring;

import javax.jws.WebService;

@WebService
public interface HelloWorld {
    String sayHi(String text);
}Our implementation will then look like this:

package demo.spring;

import javax.jws.WebService;

@WebService(endpointInterface = "demo.spring.HelloWorld")
public class HelloWorldImpl implements HelloWorld {

    public String sayHi(String text) {
        return "Hello " + text;
    }
}The @WebService annotation on the implementation class lets CXF know which interface we want to create our WSDL with. In this case its simply our HelloWorld interface.

Declaring your server beans
CXF contains support for "nice XML" within Spring 2.0. For the JAX-WS side of things, we have a <jaxws:endpoint> bean which sets up a server side endpoint.

Lets create a "beans.xml" file in our WEB-INF directory which declares an endpoint bean:

<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-extension-soap.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />

<jaxws:endpoint
  id="helloWorld"
  implementor="demo.spring.HelloWorldImpl"
  address="/HelloWorld" />
  
</beans>The bean is pretty self explanatory. The id becomes the id of the bean in the Spring context. The serviceClass property is our implementation class. Finally, the address property specifies the location which we want to host our service at. This can be either a full address with host & port or just a path.

To provide a bean name instead of a classname as an implementor, simply supply the bean-name prepended with "#", e.g. implementor="#myBean".

Setting up the Servlet
We'll need to add two things to our web.xml. First, the Spring ContextLoaderLister. This starts Spring and loads our beans.xml file. We can specify where our file is via a <context-param>. The second thing is the CXF Servlet.

<web-app>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/beans.xml</param-value>
</context-param>

<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>

<servlet>
<servlet-name>CXFServlet</servlet-name>
<display-name>CXF Servlet</display-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>It is important to note that the address that you chose for your endpoint bean must be one your servlet listens on. For instance, if my Servlet was register for "/some-services/*" but my address was "/more-services/HelloWorld", there is no way CXF could receive a request.

Create a Client
CXF includes a JaxWsProxyFactory bean which create a client for you from your service interface. You simply need to tell it what your service class is (the HelloWorld interface in this case) and the URL of your service. You can then create a client bean via the JaxWsProxyFactory bean by calling it's create() method.

Here's an example:

<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="client" class="demo.spring.HelloWorld"
      factory-bean="clientFactory" factory-method="create"/>
   
<bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
  <property name="serviceClass" value="demo.spring.HelloWorld"/>
  <property name="address" value="http://localhost:9002/HelloWorld"/>
</bean>
  
</beans>If you were going to access your client you could now simply pull it out of the Spring context (or better yet, inject it into your application using Spring!):

ApplicationContext context = ...; // your Spring ApplicationContext
HellWorld client = (HelloWorld) context.getBean("client");

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-326933-1-1.html 上篇帖子: 使用apache ab命令进行简单压力测试 下篇帖子: Apache带宽流量控制模块安装 mod_bw
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表