All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.easycode8.fastapi.core.ServiceHandlerMapping Maven / Gradle / Ivy

There is a newer version: 1.2.0
Show newest version
package com.easycode8.fastapi.core;



import com.easycode8.fastapi.core.configuration.FastApiProperties;
import com.easycode8.fastapi.core.customizer.MethodChooseCustomizer;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping;

import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.util.Arrays;
import java.util.stream.Collectors;

public class ServiceHandlerMapping extends RequestMappingInfoHandlerMapping {

    private RequestMappingInfo.BuilderConfiguration config = new RequestMappingInfo.BuilderConfiguration();

    private final FastApiProperties fastApiProperties;
    private final MethodChooseCustomizer methodChooseCustomizer;

    public ServiceHandlerMapping(FastApiProperties fastApiProperties, MethodChooseCustomizer methodChooseCustomizer) {
        this.fastApiProperties = fastApiProperties;
        this.methodChooseCustomizer = methodChooseCustomizer;
    }

    /**
     * 支持@Service注解的类
     * @param beanType
     * @return
     */
    @Override
    protected boolean isHandler(Class beanType) {
        return methodChooseCustomizer.isHandler(beanType);
    }

    /**
     * 将Service类里面的方法按照 Prefix()/SimpleClassName/methodName 的方式去注册RequestMapping
     * @param method
     * @param handlerType
     * @return
     */
    @Override
    protected RequestMappingInfo getMappingForMethod(Method method, Class handlerType) {
        if (!methodChooseCustomizer.supportsMethodType(method)) {
            return null;
        }
        String text = Arrays.stream(method.getParameters()).map(Parameter::getName).collect(Collectors.joining("-"));
        String paths = String.join("/", fastApiProperties.getPrefix(),handlerType.getSimpleName(), method.getName(), text);
        RequestMappingInfo.Builder builder = RequestMappingInfo
                .paths(paths)
                .consumes(MediaType.APPLICATION_JSON_VALUE)
                .methods(RequestMethod.POST);

        return builder.options(this.config).build();
    }

    /**
     * 保证自定义的映射优先处理
     * @return
     */
    @Override
    public int getOrder() {
        return 0;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy