1.实现效果图
2.具体代码
2.1.springboot代码
配置文件 application.yml
server:
port: 80
spring:
application:
name: CXF1
启动类
@SpringBootApplication
public class App {
public static void main(String[] args) throws Exception {
SpringApplication.run(App.class, args);
}
}
2.2.cxf配置类
@SpringBootConfiguration
public class CXFConfig {
@Autowired
private Bus bus;
@Autowired
private WebServiceInf webServiceInf;
/**
* 自定义服务名称
* 默认为 localhost/service/*
* @title cxfServlet
* @return ServletRegistrationBean
*/
@Bean
public ServletRegistrationBean cxfServlet(){
return new ServletRegistrationBean(new CXFServlet(),"/CustomServices/*");
}
/**
* 自定义接口名
* @title endpoint
* @return Endpoint
*/
@Bean
public Endpoint endpoint() {
EndpointImpl endpoint = new EndpointImpl(bus, webServiceInf);
endpoint.publish("/WebServiceInf");
return endpoint;
}
}
2.3.4个实体类
/**
* @title HeaderEnrirt.java
* @description 输入报文header中
* @time 2019/07/04 09:53:46
*/
@Data
@NoArgsConstructor
@AllArgsConstructor
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class HeaderEntity {
@XmlElement(namespace = "http://www.srrc.org.cn/")
private String appId;
@XmlElement(namespace = "http://www.srrc.org.cn/")
private String appCode;
@XmlElement(namespace = "http://www.srrc.org.cn/")
private String appName;
}
/**
* @title InputParameter.java
* @description 输入参数
* @time 2019/07/04 09:56:14
*/
@Data
@NoArgsConstructor
@AllArgsConstructor
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class InputParameter {
@XmlElement(namespace = "http://www.srrc.org.cn/")
private String userId;
@XmlElement(namespace = "http://www.srrc.org.cn/")
private String username;
@XmlElement(namespace = "http://www.srrc.org.cn/")
private String password;
}
/**
* @title OutputHeader.java
* @description 输出报文header中
* @time 2019/07/04 13:18:08
*/
@Data
@NoArgsConstructor
@AllArgsConstructor
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class OutputHeader {
@XmlElement(namespace = "http://www.srrc.org.cn/")
private String bizResCd;
@XmlElement(namespace = "http://www.srrc.org.cn/")
private String bizResText;
}
/**
* @title OutputParameter.java
* @description 输出参数
* @time 2019/07/04 09:57:17
*/
@Data
@NoArgsConstructor
@AllArgsConstructor
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class OutputParameter {
@XmlElement(namespace = "http://www.srrc.org.cn/")
private String roleId;
@XmlElement(namespace = "http://www.srrc.org.cn/")
private String roleName;
}
其中 namespace = "http://www.srrc.org.cn/" 为节点别名
2.4.cxf拦截器
/**
* @title ArtifactOutInterceptor.java
* @description 用来去掉一些属性,修改节点别名等
* @time 2019/07/03 09:23:16
*/
public class MyOutInterceptor extends AbstractPhaseInterceptor {
public MyOutInterceptor() {
super(Phase.PRE_STREAM);
}
public void handleMessage(Message message) {
try {
Map nsMap = new HashMap();
nsMap.put("http://www.srrc.org.cn/", "srrc");
JAXBDataBinding dataBinding = (JAXBDataBinding) message.getExchange().getEndpoint().getService().getDataBinding();
dataBinding.setNamespaceMap(nsMap);
Map envMap = new HashMap();
envMap.put("srrc", "http://www.srrc.org.cn/");
message.put("soap.env.ns.map", envMap);
} catch (Exception e) {
e.printStackTrace();
}
}
}
2.5.接口及其实现类
/**
* @title WebServiceInf.java
* @description 暴露的服务接口
* @time 2019/07/03 14:54:45
*/
@OutInterceptors(classes = { MyOutInterceptor.class })
@WebService(endpointInterface = "com.server.WebServiceInf", targetNamespace = "http://www.srrc.org.cn/")
public interface WebServiceInf {
@WebMethod(operationName = "getA")
@WebResult(name = "result", targetNamespace = "http://www.srrc.org.cn/")
@ResponseWrapper(localName = "verifyTheUserResponse")
public OutputParameter getAll(
@WebParam(header = true, name = "HeaderEntity", targetNamespace = "http://www.srrc.org.cn/", mode = Mode.IN) HeaderEntity headerEntity,
@WebParam(name = "InputParameter", targetNamespace = "http://www.srrc.org.cn/") InputParameter inputParameter,
@WebParam(header = true, name = "ProviderResponse", targetNamespace = "http://www.srrc.org.cn/", mode = Mode.OUT) Holder outputHeaderHolder);
}
/**
* @title WebServiceInfImpl.java
* @description 服务接口实现类
* @time 2019/07/03 14:55:14
*/
@WebService
@Service
public class WebServiceInfImpl implements WebServiceInf {
@Override
public OutputParameter getAll(HeaderEntity headerEntity, InputParameter inputParameter,
Holder outputHeaderHolder) {
OutputHeader outputHeader = new OutputHeader();
outputHeader.setBizResCd("a1");
outputHeader.setBizResText("abc");
outputHeaderHolder.value = outputHeader;
OutputParameter outputParameter = new OutputParameter();
outputParameter.setRoleId("1");
outputParameter.setRoleName("c1");
return outputParameter;
}
}
3.对照SoupUI详细说明
复杂类型如list、map等再加个转换器 XMLAdapter 就可以了。我网上找了很久,就是没有一篇cxf指定SoupUI输入输出报文的文章,就花点时间自己写了个简单的示例,以后有类似的照着改就成。
具体下载地址: https://github.com/ETNJTOTG/CXF1/