有没有办法在java中获取示例名称?

wa7juj8i  于 2021-08-25  发布在  Java
关注(0)|答案(2)|浏览(453)

这个问题在这里已经有了答案

如何获取java中的示例名称[(1份答覆)
四天前关门。
我知道这没有多大意义,但我正在尝试创建一个动态方法来记录请求,例如:

@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;
    }
mlmc2os5

mlmc2os51#

您可以为所有请求实现一个拦截器,并从请求中获取所有值
创建拦截器

@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());
    }

}

因此,当您调用时,所有的请求都将在您的实现上被拦截,并在httpservletresponse上获得方法返回或值的结果

plupiseo

plupiseo2#

无法保留您称之为变量的信息,您将该变量传递到数组中。
看到这个帖子了吗
如果你必须这么做(你不应该这么做),你应该使用Map。

相关问题