无spring框架发布CXF webservice

时间:2022-11-20 15:10:03

1、在web.xml添加

<!-- cxf配置 -->
<servlet>
<servlet-name>CXFServlet</servlet-name>
<servlet-class>com.tjx.cxf.server.WebServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/ws/*</url-pattern>
</servlet-mapping>
<!-- cxf配置 -->


2、WebServlet添加cxf webservice接口映射

import javax.servlet.ServletConfig;

import org.apache.cxf.Bus;
import org.apache.cxf.BusFactory;
import org.apache.cxf.jaxws.JaxWsServerFactoryBean;
import org.apache.cxf.transport.servlet.CXFNonSpringServlet;

import com.tjx.cxf.service.WWMH_Service;
import com.tjx.<span style="font-family: Arial, Helvetica, sans-serif;">cxf.service.YSCMPService;</span>
import com.tjx.cxf.service.impl.WWMH_ServiceImpl;
import com.tjx.cxf.service.impl.YSCMPImpl;

public class WebServlet extends CXFNonSpringServlet {

/**
*
*/
private static final long serialVersionUID = 1L;

@Override
protected void loadBus(ServletConfig servletConfig) {
super.loadBus(servletConfig);
Bus bus = getBus();
BusFactory.setDefaultBus(bus);
YSCMPImpl _YSCMPImpl = new YSCMPImpl();// 实现类
JaxWsServerFactoryBean cmpFactoryBean = new JaxWsServerFactoryBean (); // server工厂
cmpFactoryBean.setServiceClass(YSCMPService.class);// 接口类
cmpFactoryBean.setAddress("/CMP"); // 服务请求路径
cmpFactoryBean.setServiceBean(_YSCMPImpl);
cmpFactoryBean.create();

//扩展多个webservice
WWMH_ServiceImpl _WWMH_ServiceImpl = new WWMH_ServiceImpl();// 实现类
cmpFactoryBean = new JaxWsServerFactoryBean (); // server工厂
cmpFactoryBean.setServiceClass(WWMH_Service.class);// 接口类
cmpFactoryBean.setAddress("/WSHH"); // 服务请求路径
cmpFactoryBean.setServiceBean(_WWMH_ServiceImpl);
cmpFactoryBean.create();
}
}

3、编写接口类

import javax.jws.WebParam;
import javax.jws.WebService;

@WebService( serviceName="XX") //此处必须为serviceName,或则发布的服务名字会多一个Serivce 
public interface YSCMPService {
public String GETZT(@WebParam(name = "zjhm") String zjhm);
}

4、编写实现类

public class YSCMPImpl implements YSCMPService{ 
public String GETZT(String zjhm) {
return "test";
}
}


之前碰到了  cxf webservice 生成wsdl方法参数名称为arg0问题 ,详见  :http://www.cnblogs.com/Gyoung/p/5489010.html。也可以看下面粘贴过来的。

cxf webservice 生成wsdl方法参数名称为arg0问题

在通过cxf生成webservice服务时,如果你是用ServerFactoryBean,那么在生成wsdl时,方法的参数名称会被自动命名为arg0,arg1...,如:

<xsd:complexType name="addPatientRegistry">
<xsd:sequence>
<xsd:element minOccurs="0" name="arg0" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
<xsd:element name="addPatientRegistryResponse" type="tns:addPatientRegistryResponse"/>

因为,java的反序列化没办法获取参数名称。

只能用JaxWsServerFactoryBean,但在在相应的接口上加注解@WebParam

@WebService
@BindingType(value = SOAPBinding.SOAP12HTTP_BINDING)
public interface HelloService {
String hello(@WebParam(name="name")String name);
}

 HelloService hw = new HelloServiceImpl();
JaxWsServerFactoryBean jwsFactory = new JaxWsServerFactoryBean();
jwsFactory.setAddress("http://10.0.1.32:5679/hello"); //指定WebService的发布地址
jwsFactory.setServiceClass(HelloService.class);//WebService对应的类型
jwsFactory.setServiceBean(hw);//WebService对应的实现对象

jwsFactory.create();