How to get method information at Interceptor preHandle method in spring boot 1.3

时间:2016-02-03 03:42:53

标签: spring-boot

@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
    if (this.logger.isDebugEnabled()) {
        this.logger.debug(">>> handler: " + handler);
    }

    HandlerMethod handlerMethod = (HandlerMethod) handler;
    Login login = handlerMethod.getMethod().getAnnotation(Login.class);
}

I had above interceptor code in spring 3.X that works. I like to use this code in the Controller having @CrossOrigin and @RequestMapping method at spring boot 1.3. but below error is occured.

How to get method information at Interceptor preHandle method in spring boot 1.3?

Caused by: java.lang.ClassCastException: org.springframework.web.servlet.handler.AbstractHandlerMapping$PreFlightHandler cannot be cast to org.springframework.web.method.HandlerMethod

1 个答案:

答案 0 :(得分:4)

添加到部分处理请求CORS在4.2 Spring之后添加的请求将再次处理"拦截器"。 所以你可以添加代码来检查"处理程序"对象类型" HandlerMethod"类型。

e.g。

#include <stdio.h>
#include <stdlib.h>

int main (int argc, char* argv[])
{
    /*Declaring variables for rows, columns, boardsize, squaresize and pgmData 
    array*/
    int row, col,i,j;
    int iBoardSize = 800, iSquareSize = 100;
    int **iPgmData;

    /*Allocate memory for the data*/
    row = 800;
    col = 800;

    iPgmData = malloc(row * sizeof(int *));
    for (i = 0; i < row;i++)
        iPgmData[i] = malloc(col * sizeof(int));

    /*Assign data to the array the desired result is an 8x8 checkboard of 100x100
    pixel squares of alternating black and white.*/
    for (row = 0; row < iBoardSize; row++){
        for (col = 0; col < iBoardSize; col++){
            if ((row / iSquareSize + col / iSquareSize ) %2 == 0)
                iPgmData[row][col] = 0;
            else
                iPgmData[row][col] = 255;

        }
    }

    /* Open the PGM file */
    FILE* image = fopen(argv[1], "wb");
    if (image == NULL){
      fprintf(stderr, "Can't open output file %s!\n", argv[1]);
      exit(1);
    }

    /*Write the header*/
    fprintf(image, "P2\n%d %d\n255\n", iBoardSize, iBoardSize);

    for (row = 0;row <= iBoardSize; row++){
        for (col = 0;col <= iBoardSize; col++){
            if (col < iBoardSize)
                fprintf(image, "%d ", iPgmData[row][col]);
            else
                fprintf(image, "\n");
        }
    }

    /*Write pgmData*/
    fwrite(iPgmData, 1, iBoardSize*iBoardSize*sizeof(int *), image);

    /*Close the PGM*/
    fclose(image);

    /*Free allocated memory*/
    free(iPgmData);

    return 0;
}