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

com.codingapi.springboot.fast.mapping.FastMvcMappingRegister Maven / Gradle / Ivy

There is a newer version: 3.3.0.dev1
Show newest version
package com.codingapi.springboot.fast.mapping;

import jakarta.annotation.Resource;
import lombok.AllArgsConstructor;
import org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration;
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.annotation.RequestMappingHandlerMapping;
import org.springframework.web.util.pattern.PathPatternParser;

import java.lang.reflect.Method;

public class FastMvcMappingRegister {

    private final RequestMappingHandlerMapping handlerMapping;

    public FastMvcMappingRegister(
            RequestMappingHandlerMapping handlerMapping) {
        this.handlerMapping = handlerMapping;
    }

    /**
     * add mvc mapping
     *
     * @param url           mapping url
     * @param requestMethod request method
     * @param handler       executor handler
     * @param method        executor method
     */
    public void addMapping(String url, RequestMethod requestMethod, Object handler, Method method) {

        RequestMappingInfo.BuilderConfiguration options = new RequestMappingInfo.BuilderConfiguration();
        options.setPatternParser(new PathPatternParser());

        RequestMappingInfo mappingInfo = RequestMappingInfo
                .paths(url)
                .methods(requestMethod)
                .produces(MediaType.APPLICATION_JSON_VALUE)
                .options(options)
                .build();

        handlerMapping.registerMapping(mappingInfo, handler, method);
    }

    /**
     * remove mvc mapping
     *
     * @param url           mapping url
     * @param requestMethod request method
     */
    public void removeMapping(String url, RequestMethod requestMethod) {
        RequestMappingInfo.BuilderConfiguration options = new RequestMappingInfo.BuilderConfiguration();
        options.setPatternParser(new PathPatternParser());

        RequestMappingInfo mappingInfo = RequestMappingInfo
                .paths(url)
                .methods(requestMethod)
                .produces(MediaType.APPLICATION_JSON_VALUE)
                .options(options)
                .build();

        handlerMapping.unregisterMapping(mappingInfo);
    }


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy