java 中的实例名称有什么方法可以获取吗?

Is there any way to get the instance name in java?

我知道这没有多大意义,但我正在尝试创建一个动态方法来记录请求,例如:

   @GetMapping("/my-path")
   public ResponseEntity<Foo> findSomethingBy(Integer bankId, String clientName, FooFilter filter){
       log.info("The method findSomethingBy was called with=" + **dynamicMethotRequest(Arrays.asList(bankId, clientName, filter)**);

       var result = service.findSomethingBy(bankId, clientName, filter);

       log.info("The method findSomethingBy result=" + result);
       return ResponseEntity.ok(result);
   }

   @GetMapping("/my-other-path")
   public ResponseEntity<Foo> findSomethingByAnotherThing(Integer bankId, Integr clientId){
       log.info("The method findSomethingBy was called with=" + **dynamicMethotRequest(Arrays.asList(bankId, clientName)**);

       var result = service.findSomethingByAnotherThing(bankId, clientName, filter);

       log.info("The method findSomethingByAnotherThing result=" + result);
       return ResponseEntity.ok(result);
   }

来自 findSomethingBy 的第一个预期日志应该是:

The method findSomethingBy was called with=(bankId=1, clientName=Jack, filter=(age=24, job=dev, working=true, from=Brazil))

来自 findSomethingByAnotherThing 的第一个预期日志应该是:

The method findSomethingByAnotherThing was called with=(bankId=1, clientId=1)

那我怎么才能dynamicMethotRequest得到数组每个位置的实例名呢?

这是我正在尝试的方式:

private String dynamicMethotRequest(List list){
        String result = "";
        var size = list.size();
        for (int i = 0; i < size; i++) {
            var element = list.get(i);
            String elemnetName = element.getInstanceName();
            
            if(i == 0){
                result = "(";
            }
            
            result = result + "elemnetName" + "=" + element.toString();
            
            if(i+1 == size){
                result = result + ")";
        }else {
                result = result + ",";
            }
            
        }
        return result;
    }

无法保留传递给数组的变量信息。

post

如果你绝对必须这样做(你不应该这样做),你应该使用地图。

您可以为所有请求实施拦截器并从请求中获取所有值

创建拦截器


@Component
public class RequestInterceptor   implements HandlerInterceptor {

    private static final Logger LOGGER = LoggerFactory.getLogger(RequestInterceptor.class);


    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler ) throws Exception{
        doSomethingWithHandleRequest(handler, request);
        return true;
    }

    private void doSomethingWithHandleRequest(Object handler, HttpServletRequest request)throws Exception{
        
        if (handler instanceof HandlerMethod) {
            HandlerMethod method = (HandlerMethod) handler;
            LOGGER.info(method.getBeanType().getName()); // class name 
            LOGGER.info(method.getMethod().getName()); // method name
        }

        // request.getHeaderNames() to get all values from headers
        // request.getParameterNames() to get all values from parameters
        // request.getReader() to get body request with HttpServletRequestWrapper() for reading bufferreader
        // and all information on request

    }

 }

在配置中设置拦截器

@Configuration
public class WebMvcConfig  implements WebMvcConfigurer {

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new RequestInterceptor());
    }
    
}

所以当您调用时,所有请求都将在您的实现中被拦截,甚至会得到方法 return 的结果或 HttpServletResponse

上的值