RSS订阅欢迎来到Java程序员学习天地!
你的位置:首页 » 架构 » 正文

cxf 实现 webservice

选择字号: 超大 标准 发布时间:2017年05月19日 | 作者:amour505 | 0个评论 | 100人浏览

CXF 实现WebService 服务器端

http://obs-tpgkm.obs.cn-north-1.myhwclouds.com/2016/1125/16/5837f44ac5d83.png

applicationContext.xml 配置文件如下:

<beans xmlns="http://www.springfr amework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws" xsi:schemaLocation=" http://www.springfr amework.org/schema/beans http://www.springfr amework.org/schema/beans/spring-beans.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">

   

    <import resource="classpath:me ta-INF/cxf/cxf.xml"/>

    <import resource="classpath:me ta-INF/cxf/cxf-servlet.xml"/>

    <jaxws:endpoint id="card" implementor="com.xk.ccsp.exp.core.service.impl.CardServiceImpl" address="/cardService"/>

    

</beans>

spring-mvc.xml 配置文件如下:

<?xml version="1.0" encoding="UTF-8"?>  

  

<beans xmlns="http://www.springfr amework.org/schema/beans"  

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   

    xmlns:p="http://www.springfr amework.org/schema/p"  

    xmlns:context="http://www.springfr amework.org/schema/context"  

    xmlns:mvc="http://www.springfr amework.org/schema/mvc"   

    xsi:schemaLocation="http://www.springfr amework.org/schema/beans  

                            http://www.springfr amework.org/schema/beans/spring-beans-3.1.xsd  

                            http://www.springfr amework.org/schema/context   

                            http://www.springfr amework.org/schema/context/spring-context-3.1.xsd  

                            http://www.springfr amework.org/schema/mvc  

                            http://www.springfr amework.org/schema/mvc/spring-mvc-3.1.xsd">  

  

    <context:component-scan ba se-package="com.xk.ccsp.exp.core.service" />  

  

    <mvc:annotation-driven />  

</beans>  

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" 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>systemServer</display-name>

  <welcome-file-list>

    <welcome-file>index.html</welcome-file>

    <welcome-file>index.htm</welcome-file>

    <welcome-file>index.jsp</welcome-file>

    <welcome-file>default.html</welcome-file>

    <welcome-file>default.htm</welcome-file>

    <welcome-file>default.jsp</welcome-file>

  </welcome-file-list>

  

     <listener>    

        <listener-class>    

            org.springfr amework.web.context.ContextLoaderListener    

        </listener-class>    

    </listener>    

      

    <servlet>  

        <servlet-name>fr amework</servlet-name>  

        <servlet-class>org.springfr amework.web.servlet.DispatcherServlet</servlet-class>  

        <init-param>    

            <param-name>contextConfigLocation</param-name>    

            <param-value>classpath:spring-mvc.xml</param-value>    

        </init-param>    

        <load-on-startup>1</load-on-startup>  

    </servlet>  

  

    <servlet-mapping>  

        <servlet-name>fr amework</servlet-name>  

        <url-pattern>/</url-pattern>  

    </servlet-mapping>  

      

    <servlet>    

        <servlet-name>CXFServlet</servlet-name>    

        <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>

     <init-param>

        <param-name>contextConfigLocation</param-name>

         <param-value>WEB-INF/spring-mvc.xml</param-value>   

     </init-param>

        <load-on-startup>2</load-on-startup>    

    </servlet>    

    

    <servlet-mapping>    

        <servlet-name>CXFServlet</servlet-name>    

        <url-pattern>/*</url-pattern>    

    </servlet-mapping> 

    

</web-app>

jar包自己可以选择,目前我是把所有的jar包都导入进行来。

http://obs-tpgkm.obs.cn-north-1.myhwclouds.com/2016/1125/16/5837f51a7acd0.png

http://obs-tpgkm.obs.cn-north-1.myhwclouds.com/2016/1125/16/5837f535313dc.png

webService开放接口:

import javax.jws.WebService;


@WebService

public interface ICardService {


String queryCard(String json); 

}

实现类:

@WebService(endpointInterface = "com.xk.ccsp.exp.core.service.ICardService")

public class CardServiceImpl implements ICardService {

@SuppressWarnings({ "unchecked", "rawtypes" })

@Override

public String queryCard(String json) {

System.out.println("{调用省份号查询身份信息json参数为:}"+json);

ba seReturn result= new ba seReturn();

String resultStr = "";

try {

ObjectMapper mapper = new ObjectMapper();

if(StringUtils.isNotBlank(json)){

CardRequest requst = mapper.readValue(json, new TypeReference<CardRequest>() {});

if(requst == null){

ReturnUtil.errorReturn(result, "ERROR", "{解析json对象为空}");

}else{

if(requst.getCardId().equals("420621199208074857")){

  ReturnUtil.successReturn(result,getPeopleRespone());

}else{

ReturnUtil.errorReturn(result, "ERROR", "{没有证件号码为"+requst.getCardId()+"详细信息}");

}

}

}else{

ReturnUtil.errorReturn(result, "ERROR", "{传入的参数为空}");

}

resultStr = mapper.writeValueAsString(result);

} catch (IOException e) {

ReturnUtil.errorReturn(result, "ERROR", "{系统错误}:"+e.getMessage());

e.printStackTrace();

}

System.out.println("{查询身份详细信息返回结果}"+resultStr);

        return resultStr;

}


标签:webserviceJava基础架构

额 本文暂时没人评论 来添加一个吧

发表评论

必填

选填

选填

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

控制面板
您好,欢迎到访网站!
随机文章
热门文章
热评文章
最近发表