微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

CXF搭建webservice开发环境

1.web.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    id="WebApp_ID" version="2.5">
    
    <display-name>mina</display-name>
    <servlet>
        <servlet-name>mina</servlet-name>
        <servlet-class>org.springframework.web.servlet.dispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>WEB-INF/applicationContext*.xml</param-value>//引入自己的配置文件
        </init-param>
        <!-- 标识启动容器时初始化该Servlet -->
        <load-on-startup>1</load-on-startup>
    </servlet>

    
    <servlet>
        <servlet-name>CXFServlet</servlet-name>
        <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>CXFServlet</servlet-name>
        <url-pattern>/pubService/*</url-pattern>   //ws根路径
    </servlet-mapping>

 <listener>  
      <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  //很重要
    </listener>  
    
    </web-app>

2.application.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"    
    xmlns:jaxrs="http://cxf.apache.org/jaxrs"    
    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    
    http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd">    
    
    
    <import resource="classpath:meta-inf/cxf/cxf.xml" />
    <import resource="classpath:meta-inf/cxf/cxf-servlet.xml" />
    
    
   <!--  <jaxws:endpoint id="pubService" implementor="com.edu.webservice.impl.EduWebServiceImpl" 
address="/pubService"></jaxws:endpoint> -->
    <!--自己的服务接口-->
    <bean id="eduService" class="com.edu.webservice.impl.EduWebServiceImpl"></bean>
    
    <jaxrs:server id="eduWebService"   address="/eduWebService">    
        <jaxrs:serviceBeans>    
            <ref local="eduService"/>    
        </jaxrs:serviceBeans>    
        <jaxrs:extensionMappings>    
            <entry key="json" value="application/json" />    
            <entry key="xml" value="application/xml" />    
        </jaxrs:extensionMappings>    
    </jaxrs:server>    



</beans>

3.服务接口:

import javax.ws.rs.Consumes;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;



@Path("")
public interface IEduWebService {
    
    @GET
    @Path("/test")
     public String sayHello();  
     
    @GET
     @Path("/say/{name}")
     public String sayHelloWithName(@PathParam("name")String name);  
    
    @GET
    @Path("/getEdus")
    public String getEducations();
    
    
    @GET
    @Path("/print")
    @Consumes(MediaType.APPLICATION_FORM_URLENCODED)  
    @Produces(MediaType.APPLICATION_JSON) 
    public String printEdu(@FormParam("id")Integer id,@FormParam("education")String education,@FormParam("currentDate")String currentDate);
    
}

4.实现接口,启动服务器,地址栏输入:http://127.0.0.1:8080/mina/pubService?wsdl即可看到自己发布的ws

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐