Java webService服务端封装

使用java语言,封装webService服务端。
使用@WebService和@WebParam注解
第一步:创建WebServiceConfig

package com.hfi.health.further.visit.config.web;

import com.hfi.health.further.visit.config.web.impl.HisWebServiceImpl;
import org.apache.cxf.Bus;
import org.apache.cxf.bus.spring.SpringBus;
import org.apache.cxf.jaxws.EndpointImpl;
import org.apache.cxf.transport.servlet.CXFServlet;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.xml.ws.Endpoint;

@Configuration
public class WebServiceConfig {

    @Bean
    public ServletRegistrationBean disServlet() {
        ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new CXFServlet(), "/webService/*");
        return servletRegistrationBean;
    }
    @Bean(name = Bus.DEFAULT_BUS_ID)
    public SpringBus springBus() {
        return new SpringBus();
    }
    @Bean
    public Endpoint endpoint() {
        EndpointImpl endpoint = new EndpointImpl(springBus(), new HisWebServiceImpl());
        endpoint.publish("/sch");
        return endpoint;
    }

}

第二步:创建接口
 

package com.hfi.health.further.visit.config.web;

import com.hfi.health.further.visit.api.form.res.BaseResForm;
import com.hfi.health.further.visit.config.web.req.CheckLabReqFrom;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;

@WebService(name = "HisWebService",
        targetNamespace = "http://NoticeInterface/")
public interface HisWebService {


    @WebMethod
    BaseResForm resourceMethod(@WebParam(name = "xmlData",targetNamespace = "http://NoticeInterface/") String jsonData);
}

第三步:创建接口实现类

package com.hfi.health.further.visit.config.web.impl;

import com.hfi.health.further.visit.api.form.res.BaseResForm;
import com.hfi.health.further.visit.config.web.HisWebService;
import com.neusoft.niox.support.jaxb.JAXBUtils;
import jt.his.sdk.webservice.dto.resp.RespMessages;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.jws.WebParam;
import javax.jws.WebService;

@Slf4j
@Component
@WebService(name = "HisWebService",
        targetNamespace = "http://NoticeInterface/",
        endpointInterface = "com.hfi.health.further.visit.config.web.HisWebService")
public class HisWebServiceImpl implements HisWebService {
    @Override
    public BaseResForm resourceMethod(@WebParam(name = "xmlData",targetNamespace = "http://NoticeInterface/") String xmlData) {
        log.info("his调用入参::{}", xmlData);
        try {
            RespMessages respMessages = JAXBUtils.unmarshal(xmlData, RespMessages.class);
            return BaseResForm.success();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return BaseResForm.success();
    }

}

注意:如果出现变量是sch:xmlData形式的话,@WebParam需要添加命名空间targetNamespace
以该请求入参为例:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:sch="http://NoticeInterface/">

<soapenv:Header/>

<soapenv:Body>

<sch:resourceMethod>

<sch:xmlData>

<![CDATA[<messages><heartbeat>0</heartbeat></messages>]]>

</sch:xmlData>

</sch:resourceMethod>

</soapenv:Body>

</soapenv:Envelope>

public BaseResForm resourceMethod(@WebParam(name = "xmlData",targetNamespace = "http://NoticeInterface/") String xmlData)
resourceMethod为方法名,targetNamespace可以指定sch:xmlData中的sch:,name执行写xmlData就能获取到<messages><heartbeat>0</heartbeat></messages>了

Java WebService 简单实例 http://blog.youkuaiyun.com/kingmax54212008/article/details/49764401 前言:朋友们开始以下教程前,请先看第五大点的注意事项,以避免不必要的重复操作。 一、准备工作(以下为本实例使用工具) 1、MyEclipse10.7.1 2、JDK 1.6.0_22 二、创建服务端 1、创建【Web Service Project】,命名为【TheService】。 2、创建【Class】类,命名为【ServiceHello】,位于【com.hyan.service】包下。 3、编写供客户端调用的方法,即编译方法代码。 4、进行编译 说明:编译失败的话,请将该项目引用的jdk设置为1.6.0_17版本或更高版本 5、测试结果 测试地址:http://localhost:9001/Service/ServiceHello?wsdl 三、生成客户端 1、创建【Web Service Project】,命名为【TheClient】。 2、命令提示窗口执行生成命令。 格式:wsimport -s "src目录" -p “生成类所在包名” -keep “wsdl发布地址” 示例: wsimport -s G:\\workspace\\webService\\TheClient\\src -p com.hyan.client -keep http://localhost:9001/Service/ServiceHello?wsdl 说明: 1)"src目录"地址不可含空格 2)“wsdl发布地址”不要漏了“?wsdl” 3、刷新项目,检查生成类 四、最终测试 1、创建【Class】类,命名为【ServiceTest】,位于【com.hyan.test】包下。 2、编写测试方法,调用服务端方法,并打印返回的结果。 五、注意事项 1、JDK版本过低问题 报类似如下的异常:runtime modeler error: Wrapper class com.hyan.service.jaxws.GetValue is not found. Have you run APT to generate them? 原因:JDK版本低于1.6.0_17 解决方法:调整该服务端项目所引用的JDK版本为安装的高版本JDK
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值