springboot(2.1.6)+cxf(3.3.2)的注解开发(实现任意修改SoupUI的输入输出报文)

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<CXFServlet>
	 */
	@Bean
    public ServletRegistrationBean<CXFServlet> cxfServlet(){
        return new ServletRegistrationBean<CXFServlet>(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<Message> {

    public MyOutInterceptor() {
        super(Phase.PRE_STREAM);
    }

    public void handleMessage(Message message) {
        try {
            Map<String, String> nsMap = new HashMap<String, String>();
            nsMap.put("http://www.srrc.org.cn/", "srrc");
            JAXBDataBinding dataBinding = (JAXBDataBinding) message.getExchange().getEndpoint().getService().getDataBinding();
            dataBinding.setNamespaceMap(nsMap);

            Map<String, String> envMap = new HashMap<String, String>();
            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<OutputHeader> 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<OutputHeader> 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/

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值