spring的WebService开发

引入的包,这里不多说了,自己去下载cxf的zip文件,解压里面都有.
1.创建一个接口
@WebService
public interface HelloWorldService {
 @WebMethod
  public String getNewName(String userName);
 @WebMethod
  public Map<String,String> getMap(String key,String value);
}

使用@WebService标识让CXF知道使用该接口来创建WSDL.
2.创建实现类
@WebService
public class HelloWorldServiceImpl implements HelloWorldService {
 @Override
 public String getNewName(String userName) {
  return "Hello Spring!" + userName;
 }
 @Override
 public Map<String, String> getMap(String key,String value) {
  Map<String,String> tempMap = new HashMap<>();
  tempMap.put(key,value );
  return tempMap;
 }
}
3.编写spring的配置文件
<?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="hselloWorldService" 
  implementor="com.server.HelloWorldServiceImpl" address="/helloworld" />
</beans>
或者
<bean id="hselloWorldService" class="com.server.HelloWorldServiceImpl" />
<jaxws:endpoint 
         id="hselloWorldService" 
         implementor="#hselloWorldService" 
           address="/helloworld" />
4.web.xml文件配置

<!-- Spring Config Location -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath*:cxf-servlet.xml</param-value>
    </context-param>
    <!-- Spring ContextLoaderListener -->
    <listener>
        <listener-class>
            org.springframework.web.context.ContextLoaderListener
        </listener-class>
    </listener>
 <!-- Apache CXFServlet -->
    <servlet>
        <servlet-name>CXFServlet</servlet-name>
        <servlet-class>
            org.apache.cxf.transport.servlet.CXFServlet
        </servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <!-- CXFServlet Mapping -->
    <servlet-mapping>
        <servlet-name>CXFServlet</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>
以上配置完毕,则启动项目输入项目路径+helloworld?wsdl.既可以看到wsdl信息,然后用wsdl2java或者wsimport命令生成客户端代码就完成了.

你可能感兴趣的:(spring,webservice,CXF)