-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(scaffold): provide uniform return body handler
- Loading branch information
Showing
6 changed files
with
84 additions
and
0 deletions.
There are no files selected for viewing
70 changes: 70 additions & 0 deletions
70
...artifactId}-provider/src/main/java/${basePath}/config/web/ReturnValueConfigurer.tmpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package ${basePackage}.config.web; | ||
|
||
import ${basePackage}.controller.Payload; | ||
import org.springframework.beans.factory.InitializingBean; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.core.MethodParameter; | ||
import org.springframework.core.annotation.AnnotationUtils; | ||
import org.springframework.web.context.request.NativeWebRequest; | ||
import org.springframework.web.method.support.HandlerMethodReturnValueHandler; | ||
import org.springframework.web.method.support.ModelAndViewContainer; | ||
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter; | ||
import org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
@Configuration | ||
public class ReturnValueConfigurer implements InitializingBean { | ||
@Autowired | ||
private RequestMappingHandlerAdapter adapter; | ||
|
||
@Override | ||
public void afterPropertiesSet() { | ||
List<HandlerMethodReturnValueHandler> handlers = new ArrayList<>(adapter.getReturnValueHandlers()); | ||
for (HandlerMethodReturnValueHandler item : handlers) { | ||
int index = handlers.indexOf(item); | ||
if (RequestResponseBodyMethodProcessor.class.isAssignableFrom(item.getClass())) { | ||
handlers.add(index, new RequestResponseBodyMethodProcessorProxy((RequestResponseBodyMethodProcessor) item)); | ||
handlers.remove(item); | ||
break; | ||
} | ||
} | ||
adapter.setReturnValueHandlers(handlers); | ||
} | ||
|
||
private static class RequestResponseBodyMethodProcessorProxy implements HandlerMethodReturnValueHandler { | ||
private RequestResponseBodyMethodProcessor delegate; | ||
|
||
public RequestResponseBodyMethodProcessorProxy(RequestResponseBodyMethodProcessor delegate) { | ||
this.delegate = delegate; | ||
} | ||
|
||
@Override | ||
public boolean supportsReturnType(MethodParameter methodParameter) { | ||
if (!delegate.supportsReturnType(methodParameter)) { | ||
return false; | ||
} | ||
|
||
if (AnnotationUtils.findAnnotation(methodParameter.getMethod(), Payload.class) != null) { | ||
return true; | ||
} else { | ||
Class<?> clazz = methodParameter.getContainingClass(); | ||
return AnnotationUtils.findAnnotation(clazz, Payload.class) != null; | ||
} | ||
} | ||
|
||
@Override | ||
public void handleReturnValue(Object o, MethodParameter methodParameter, ModelAndViewContainer modelAndViewContainer, NativeWebRequest nativeWebRequest) throws Exception { | ||
Map<String, Object> result = new HashMap<>(); | ||
result.put("success", true); | ||
result.put("code", "1"); | ||
result.put("payload", o); | ||
|
||
delegate.handleReturnValue(result, methodParameter, modelAndViewContainer, nativeWebRequest); | ||
} | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
...p/templates/${artifactId}-provider/src/main/java/${basePath}/controller/Payload.tmpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package ${basePackage}.controller; | ||
|
||
import java.lang.annotation.*; | ||
|
||
@Target({ElementType.TYPE, ElementType.METHOD}) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Documented | ||
public @interface Payload { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters