添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

前景:

因项目中发布了80+个接口,需要给每个接口加上请求和响应的日志信息。(PS:没日志很蛋疼老是背锅。)给这些接口加上日志信息的同时还必须满足接口功能不受影响。

方案:

根据研究发现可以使用xfire的handler拦截器实现,原理见如下请求示意图。

xfire接口请求示意图

具体实现:

1.编写JAVA类 继承 org.codehaus.xfire.handler.AbstractHandler 重写 invoke方法。

inHandler


import com.alibaba.fastjson.JSON;
import com.fastunit.util.DateUtil;
import java.lang.reflect.Method;
import org.codehaus.xfire.MessageContext;
import org.codehaus.xfire.exchange.MessageExchange;
import org.codehaus.xfire.handler.AbstractHandler;
import org.codehaus.xfire.service.OperationInfo;
public class WebserviceServerHandler extends AbstractHandler {
	public void invoke(MessageContext context) throws Exception {
		try {
			print(context);
		} catch (Exception localException) {
	public static void print(MessageContext context) {
		try {
			MessageExchange exchange = context.getExchange();
			OperationInfo opinfo = exchange.getOperation();
			Method requestM = opinfo.getMethod();//获取到客户端请求的接口对象
			//context.getInMessage().getBody() 这里返回的是客户端发送请求的信息
			System.out.println();
			System.out.println();
			//输出请求的接口名称
			System.out.println("===============================" 
			+ DateUtil.getCurrentDatetime() + "====BEGIN REQUEST "+ requestM.getName() 
							 + "=========================================");
			//输出请求的接口参数
			System.out.println("REQUEST PARAMS:\n[METHOD=" + requestM.getName() + "]:\t"
					+ JSON.toJSONString(context.getInMessage().getBody()));
		} catch (Exception localException) {

outHandler

import com.alibaba.fastjson.JSON;
import com.fastunit.util.DateUtil;
import java.lang.reflect.Method;
import org.codehaus.xfire.MessageContext;
import org.codehaus.xfire.exchange.MessageExchange;
import org.codehaus.xfire.handler.AbstractHandler;
import org.codehaus.xfire.service.OperationInfo;
public class WebserviceServerOutHandler extends AbstractHandler {
	public void invoke(MessageContext context) throws Exception {
		try {
			print(context);
		} catch (Exception localException) {
	public void print(MessageContext context) {
		MessageExchange exchange = context.getExchange();
		OperationInfo opinfo = exchange.getOperation();
		Method responseM = opinfo.getMethod();
		System.out.println("RESPONSE MSG:\n[METHOD = " + responseM.getName() + "]:\t"
				+ JSON.toJSONString(context.getOutMessage().getBody()));
		System.out.println("===============================" + DateUtil.getCurrentDatetime() + "====OVER RESPONSE "
				+ responseM.getName() + "=========================================");

2.配置xfire拦截器

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://xfire.codehaus.org/config/1.0">
    <service>
          <name>services</name>
          <namespace>http://xxx.com</namespace>
          <serviceClass>com.xxxxx.IServices</serviceClass>
          <implementationClass>com.xxxxx.ServicesImpl</implementationClass>
		  <inHandlers>  
            <handler handlerClass="com.xxx.WebserviceServerHandler"/>  
          </inHandlers>  
		  <outHandlers>  
            <handler handlerClass="com.xxx.WebserviceServerOutHandler"/>  
          </outHandlers> 
          <style>wrapped</style>
          <use>literal</use>
          <scope>application</scope>
     </service>
</beans>

3.显示效果

===============================2018-09-19 04:53:27====BEGIN REQUEST connectHost=========================================
REQUEST PARAMS:
[METHOD=connectHost]:	[{"code":"13476165039140","encryption":"0","expiration":"2099-12-31","groupid":"0","host":"218.70.82.202","port":"1000","signature":"302c021405d2a181d2cfdd69659b9599b24e949539eba5d6021458ed9671e6ebe72da05bf7507e5910c95b943a92","version":"1.0.6"},"plmuser","123456","2","5",""]
RESPONSE MSG:
[METHOD = connectHost]:	["1537304007780408"]
===============================2018-09-19 04:53:27====OVER RESPONSE connectHost=========================================
版权声明:本文为CSDN博主「weixin_40123560」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_40123560/article/details/82791644