Skip to content

Commit

Permalink
feat(scaffold): provide uniform return body handler
Browse files Browse the repository at this point in the history
  • Loading branch information
taccisum committed Jul 9, 2019
1 parent 8a96d66 commit acb3ab1
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 0 deletions.
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);
}
}
}
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 {
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

@RestController
@RequestMapping("demo")
@Payload
public class DemoController {
@Autowired
private DemoService service;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

@RestController
@RequestMapping("demo/mybatis")
@Payload
public class CrudDemoController {
@Autowired
private CrudDemoService service;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

@RestController
@RequestMapping("demo/openfeign")
@Payload
public class OpenFeignDemoController {
@Autowired
private DemoFeignClient client;
Expand Down
2 changes: 2 additions & 0 deletions generators/test/app/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ describe('generate app', () => {
it('should exists java files', () => {
assert.file('foo-service-provider/src/main/java/com/deepexi/foo/StartupApplication.java')
assert.file('foo-service-provider/src/main/java/com/deepexi/foo/config/ApplicationConfiguration.java')
assert.file('foo-service-provider/src/main/java/com/deepexi/foo/config/web/ReturnValueConfigurer.java')
assert.file('foo-service-provider/src/main/java/com/deepexi/foo/controller/Payload.java')
assert.file('foo-service-provider/pom.xml')
})

Expand Down

0 comments on commit acb3ab1

Please sign in to comment.