com.jn.agileway.springboot.web.rest.GlobalRestHandlersConfiguration Maven / Gradle / Ivy
package com.jn.agileway.springboot.web.rest;
import com.jn.agileway.http.rest.*;
import com.jn.agileway.http.rest.exceptionhandler.ThrowableHandler;
import com.jn.agileway.http.rr.requestmapping.JavaMethodRequestMappingAccessorParser;
import com.jn.agileway.http.rr.requestmapping.RequestMappingAccessorRegistry;
import com.jn.agileway.spring.web.mvc.requestmapping.SpringRequestMappingAccessorParser;
import com.jn.agileway.spring.web.rest.EasyjsonHttpMessageConverter;
import com.jn.agileway.spring.web.rest.GlobalSpringRestExceptionHandler;
import com.jn.agileway.spring.web.rest.GlobalSpringRestResponseBodyAdvice;
import com.jn.agileway.spring.web.rest.GlobalSpringRestResponseBodyHandler;
import com.jn.agileway.web.filter.globalresponse.GlobalFilterRestExceptionHandler;
import com.jn.agileway.web.filter.globalresponse.GlobalFilterRestResponseHandler;
import com.jn.agileway.web.filter.globalresponse.GlobalRestResponseFilter;
import com.jn.easyjson.core.JSONFactory;
import com.jn.langx.util.Strings;
import com.jn.langx.util.collection.Collects;
import com.jn.langx.util.function.Consumer;
import com.jn.langx.util.function.Function;
import com.jn.langx.util.function.Predicate2;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.core.annotation.Order;
import java.util.List;
import java.util.Map;
@Configuration
@Import({SpringBuiltinExceptionHandlerAutoConfiguration.class})
@AutoConfigureBefore(GlobalSpringRestResponseBodyAdvice.class)
public class GlobalRestHandlersConfiguration {
@Bean
public GlobalFilterRestResponseHandler filterRestResponseHandler(
GlobalRestResponseBodyContext context
) {
GlobalFilterRestResponseHandler filterRestResponseHandler = new GlobalFilterRestResponseHandler();
filterRestResponseHandler.setContext(context);
filterRestResponseHandler.init();
return filterRestResponseHandler;
}
@Order(-90)
@Bean
@Autowired
public FilterRegistrationBean globalRestFilterRegister(
GlobalFilterRestExceptionHandler globalFilterRestExceptionHandler,
GlobalFilterRestResponseHandler filterRestResponseHandler) {
FilterRegistrationBean registration = new FilterRegistrationBean();
GlobalRestResponseFilter filter = new GlobalRestResponseFilter();
registration.setFilter(filter);
filter.setRestResponseBodyHandler(filterRestResponseHandler);
filter.setExceptionHandler(globalFilterRestExceptionHandler);
registration.setUrlPatterns(Collects.newArrayList("/*"));
registration.setName("GlobalRestResponse Filter");
return registration;
}
@Bean
@ConditionalOnMissingBean({SpringRequestMappingAccessorParser.class})
public SpringRequestMappingAccessorParser springRequestMappingAccessorParser() {
return new SpringRequestMappingAccessorParser();
}
@Bean
@Autowired
@ConditionalOnMissingBean({GlobalRestResponseBodyHandlerConfiguration.class})
public GlobalRestResponseBodyHandlerConfiguration globalRestResponseBodyHandlerConfiguration(
GlobalRestProperties properties
) {
if (properties.getGlobalExceptionHandler().isWriteUnifiedResponse()) {
String errorControllerClass = SpringBootErrorControllers.getErrorController();
if (Strings.isNotBlank(errorControllerClass)) {
properties.getGlobalResponseBody().addAssignableType(errorControllerClass);
}
}
GlobalRestResponseBodyHandlerConfiguration configuration = new GlobalRestResponseBodyHandlerConfigurationBuilder()
.setProperties(properties.getGlobalResponseBody())
.build();
return configuration;
}
@Bean
@ConditionalOnMissingBean
public ThrowableHandler throwableHandler() {
return new ThrowableHandler();
}
@Bean
@ConditionalOnMissingBean
public RequestMappingAccessorRegistry requestMappingAccessorRegistry(ObjectProvider> requestMappingAccessorParserProviders) {
final RequestMappingAccessorRegistry registry = new RequestMappingAccessorRegistry();
Collects.forEach(requestMappingAccessorParserProviders.getIfAvailable(), new Consumer() {
@Override
public void accept(JavaMethodRequestMappingAccessorParser parser) {
registry.addParser(parser);
}
});
registry.init();
return registry;
}
@Bean
@ConditionalOnMissingBean
public GlobalRestResponseBodyMapper globalRestResponseBodyMapper(
@Autowired
GlobalRestResponseBodyHandlerConfiguration configuration,
@Autowired(required = false)
@Qualifier("globalRestResponseFieldsCleaner")
Predicate2 fieldsCleaner,
@Autowired(required = false)
@Qualifier("globalRestResponseFieldsMapper")
Function
© 2015 - 2025 Weber Informatics LLC | Privacy Policy