CXF_HOEM=E:\apache-cxf-2.2.2 CLASSPATH=%CXF_HOME%/lib; PATH=%CXF_HOME%/bin; 配置这两个参数用途与配置JAVA变量一下,在DOS窗口下直接运行java2ws,wsdl2java等可执行文件。当然你没有配置也可以进到CXF框架的bin目录下远行这个几个可执行文件。 %JAVA_HOME%/jre/lib目录下创建一下endorsed文件夹,将jaxb-api.jar,jaxws.jar(geronimo-jaxws_2.1_spec-1.0.jar,jaxb-api-2.1.jar)拷贝进去。现在再运一下java2ws,成功运行,配置生效了。 cmd进入DOS环境下输入wsdl2java -p com.cneport.service(自定义包名) http://IP:port/project name/Service?wsdl(前提是服务器没停止)
<?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/schemas/jaxws.xsd"> <jaxws:client id="service" address="http://localhost:8080/cxf/Service" serviceClass="com.cneport.service.IService" /> </beans>
public class SampleClient { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext( "beans.xml"); IService service = (IService) context.getBean("service"); ServerUserInfo userInfo = new ServerUserInfo(); userInfo.setAddress(1); userInfo.setAge(100); userInfo.setName("China"); service.save(userInfo, true); service.delete(1); } }
<?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/schemas/jaxws.xsd"> <!--导入与CXF框架有关的xml--> <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" /> <!--发布WebService接口--> <jaxws:endpoint id="service" implementor="com.cneport.service.ServiceImpl" address="/Service"> </jaxws:endpoint> </beans>
@XmlType(name = "ServerUserInfo") @XmlAccessorType(XmlAccessType.FIELD) public class UserInfoDTO implements java.io.Serializable { private static final long serialVersionUID = -4666026219400887433L; private Integer id; private String name; private Integer age; private Integer address; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public Integer getAddress() { return address; } public void setAddress(Integer address) { this.address = address; } public UserInfoDTO() { } }
<web-app> <context-param> <param-name>contextConfigLocation</param-name> <param-value> /WEB-INF/classes/applicationContext-*.xml, /WEB-INF/classes/webservice.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>
@WebService public interface IService { public void save(@WebParam(name = "dto")UserInfoDTO dto, @WebParam(name = "flag")boolean flag); public void update(@WebParam(name = "info")String xml); public void delete(@WebParam(name = "id")int id); public @WebResult(name = "String")String get(@WebParam(name = "id")int id); }
@WebService public class ServiceImpl implements IService { private Logger log = LoggerFactory.getLogger(ServiceImpl.class); public void delete(int id) { log.info("delete id is {} user" + id); } public void save(UserInfoDTO dto, boolean flag) { System.out.println("name:" + dto.getName()); } public void update(String xml) { } public String get(int id) { return null; } }