一,Java Application实现CXF Soap调用
准备
1.我们需要吧client依赖的jar包导入
2.服务端我们按照 https://my.oschina.net/ososchina/blog/337243编写
3.通过wsdl2java 命令生成代码
cmd进入src目录,执行类似如下命令,便可以生成wsdl的stub工具类
wsdl2java -d ./ http://localhost:8080/SpringCFXServer/WebService/helloWorld?wsdl
代码编写
public static void main(String[] args){
//System.setProperty("http.proxySet", "true");
//System.setProperty("http.proxyHost", "127.0.0.1"); //此处代码用于http抓包,和程序无关
//System.setProperty("http.proxyPort", "9898");
HelloWorldImplService factory=new HelloWorldImplService();
//此处返回的只是远程Web Service的代理
HelloWorld hw=factory.getHelloWorldImplPort();
/**
* 添加的拦截器
*/
Client client=ClientProxy.getClient(hw);
//参数为输入的用户名,密码
client.getOutInterceptors().add(new AddHeaderInterceptor("hejingyuan","hjy"));
System.out.println(hw.sayHi("hejingyuan"));
System.out.println("--------------------------");
User user=new User();
user.setId(20);
user.setName("孙悟空");
user.setPass("111");
user.setAddress("花果山");
List<Cat> cats=hw.getCatsByUser(user);
for(Cat cat:cats){
System.out.println(cat.getName());
}
System.out.println("--------------------------");
System.out.println(hw.getAllCats().getEntry().get(0).getKey());
System.out.println(hw.getAllCats().getEntry().get(0).getValue().getName());
}
一,Java Web+Spring实现CXF Soap调用
1.进入src目录使用命令生成CXF Stub
wsdl2java -d ./ http://localhost:8080/SpringCFXServer/WebService/userService?wsdl
wsdl2java -d ./ http://localhost:8080/SpringCFXServer/WebService/helloWorld?wsdl
2.配置web.xml
<?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">
<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>
</web-app>
3.配置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-extension-soap.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
<bean id="listCatsAction" class="com.tgb.action.ListCatsAction">
<property name="helloWorld" ref="helloWorld"></property>
</bean>
<!-- 配置远程webservice代理 -->
<!-- id="helloWorld"对应 action setHelloWorld() -->
<jaxws:client id="helloWorld"
serviceClass="com.tgb.service.HelloWorld"
address="http://localhost:8080/SpringCFXServer/WebService/helloWorld">
<jaxws:outInterceptors>
<!-- 配置输出拦截器 -->
<bean class="com.tgb.auth.AddHeaderInterceptor" >
<constructor-arg value="admin"/>
<constructor-arg value="admin"/>
</bean>
</jaxws:outInterceptors>
</jaxws:client>
<jaxws:client id="userService"
serviceClass="com.tgb.service.HelloWorld"
address="http://localhost:8080/SpringCFXServer/WebService/userService">
<jaxws:outInterceptors>
<!-- 配置输出拦截器 -->
<bean class="com.tgb.auth.AddHeaderInterceptor" >
<constructor-arg value="hejingyuan"/>
<constructor-arg value="hjy"/>
</bean>
</jaxws:outInterceptors>
</jaxws:client>
</beans>
jaxws:client 同样会把服务声明成为java对象,我们只需要在特定的Controller中注入即可。
public class ListCatsController{
private HelloWorld helloWorld;
public HelloWorld getHelloWorld() {
return helloWorld;
}
public void setHelloWorld(HelloWorld helloWorld) {
this.helloWorld = helloWorld;
}
}
4.AddHeaderInterceptor类的实现
public class AddHeaderInterceptor extends AbstractPhaseInterceptor<SoapMessage>{
private String userId;
private String userPass;
public AddHeaderInterceptor(String userId,String userPass){
super(Phase.PREPARE_SEND);//在准备发送SOAP消息时启用该拦截器
this.userId=userId;
this.userPass=userPass;
}
@Override
public void handleMessage(SoapMessage msg) throws Fault {
List<Header> headers=msg.getHeaders();
//创建Document对象
Document doc=DOMUtils.createDocument();
Element ele=doc.createElement("authHeader");
//此处创建的元素应该按照服务器那边的要求
Element idEle=doc.createElement("userId");
idEle.setTextContent(userId);
Element passEle=doc.createElement("userPass");
passEle.setTextContent(userPass);
ele.appendChild(idEle);
ele.appendChild(passEle);
/**
* 上面代码生成了一个如下XML文档片段
* <authHeader>
* <userId>hejingyuan</userId>
* <userPass>hjy</userPass>
* </authHeader>
*/
//把ele元素包装成Header,并添加到SOAP消息的Header列表中
//(QName就是一个带有命名空间的节点)
headers.add(new Header(new QName("hejingyuan"),ele));
}
}
5.restful webservice代码编写
@Path("/rest")
public interface MyRestfulService {
@Path("/add/{x}/{y}")
@GET
@Produces({ MediaType.APPLICATION_JSON +";charset=UTF-8"})
public Map<String,String> add(@PathParam("x")int i,@PathParam("y")int y);
@POST
@Path("/sayHi/{msg}")
@Produces({MediaType.APPLICATION_JSON +";charset=UTF-8"})
public Map<String, String> put(@PathParam("msg")String id);
}