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