VSResponseHandlerConfig fix

This commit is contained in:
oyo
2025-02-11 18:44:16 +08:00
parent 05133a7e40
commit af972d2c14

View File

@@ -1,20 +1,42 @@
package com.vs; package com.vs;
import com.vs.common.util.rpc.handler.CustomArgumentResolver; import com.vs.common.util.rpc.handler.CustomArgumentResolver;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.web.method.annotation.MapMethodProcessor;
import org.springframework.web.method.support.HandlerMethodArgumentResolver; import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.HandlerMethodReturnValueHandler; import org.springframework.web.method.support.HandlerMethodReturnValueHandler;
import org.springframework.web.servlet.HandlerExceptionResolver; import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;
import org.springframework.web.servlet.mvc.method.annotation.ViewNameMethodReturnValueHandler;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List; import java.util.List;
@Configuration @Configuration
public class VSResponseHandlerConfig implements WebMvcConfigurer { public class VSResponseHandlerConfig implements WebMvcConfigurer {
@Value("${com.toco.response.handler:true}") private boolean useHandler = true;
private boolean useHandler; RequestMappingHandlerAdapter requestMappingHandlerAdapter;
void init() {
List<HandlerMethodReturnValueHandler> returnValueHandlers = this.requestMappingHandlerAdapter.getReturnValueHandlers();
Iterator<HandlerMethodReturnValueHandler> iterator = returnValueHandlers.iterator();
List<HandlerMethodReturnValueHandler> newProcessors = new ArrayList<>();
while (iterator.hasNext()) {
HandlerMethodReturnValueHandler next = iterator.next();
//在controller中直接返回map不被默认的MapMethodProcessor拦截
if (next instanceof MapMethodProcessor ||
//在controller中直接返回String不被默认的ViewNameMethodReturnValueHandler拦截
next instanceof ViewNameMethodReturnValueHandler) {
continue;
} else {
newProcessors.add(next);
}
}
this.requestMappingHandlerAdapter.setReturnValueHandlers(newProcessors);
}
@Override @Override
public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) { public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) {