加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

CXF搭建webservice开发环境

发布时间:2020-12-16 23:22:18 所属栏目:安全 来源:网络整理
导读: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:schemaLocatio

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读