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>了

  • 12
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值