Jelajahi Sumber

优化响应包装器

zhou-hao 5 tahun lalu
induk
melakukan
7f9672158c

+ 6 - 0
hsweb-commons/hsweb-commons-crud/pom.xml

@@ -19,6 +19,12 @@
             <version>${project.version}</version>
         </dependency>
 
+        <dependency>
+            <groupId>org.springframework</groupId>
+            <artifactId>spring-webflux</artifactId>
+            <optional>true</optional>
+        </dependency>
+
         <dependency>
             <groupId>io.projectreactor</groupId>
             <artifactId>reactor-core</artifactId>

+ 28 - 0
hsweb-commons/hsweb-commons-crud/src/main/java/org/hswebframework/web/crud/web/CommonWebFluxConfiguration.java

@@ -0,0 +1,28 @@
+package org.hswebframework.web.crud.web;
+
+import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
+import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.core.ReactiveAdapterRegistry;
+import org.springframework.http.codec.ServerCodecConfigurer;
+import org.springframework.web.reactive.accept.RequestedContentTypeResolver;
+
+@Configuration
+@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.REACTIVE)
+public class CommonWebFluxConfiguration {
+
+    @Bean
+    public CommonErrorControllerAdvice commonErrorControllerAdvice(){
+        return new CommonErrorControllerAdvice();
+    }
+
+
+    @Bean
+    @ConditionalOnProperty(prefix = "hsweb.webflux",name = "response-wrapper",havingValue = "enabled",matchIfMissing = true)
+    public ResponseMessageWrapper responseMessageWrapper(ServerCodecConfigurer codecConfigurer,
+                                                         RequestedContentTypeResolver resolver,
+                                                         ReactiveAdapterRegistry registry){
+        return new ResponseMessageWrapper(codecConfigurer.getWriters(),resolver,registry);
+    }
+}

+ 2 - 0
hsweb-commons/hsweb-commons-crud/src/main/java/org/hswebframework/web/crud/web/ResponseMessage.java

@@ -1,5 +1,6 @@
 package org.hswebframework.web.crud.web;
 
+import com.fasterxml.jackson.annotation.JsonInclude;
 import lombok.*;
 
 import java.io.Serializable;
@@ -9,6 +10,7 @@ import java.io.Serializable;
 @Builder
 @AllArgsConstructor
 @NoArgsConstructor
+@JsonInclude(JsonInclude.Include.NON_NULL)
 public class ResponseMessage<T> implements Serializable {
 
     private static final long serialVersionUID = 8992436576262574064L;

+ 55 - 0
hsweb-commons/hsweb-commons-crud/src/main/java/org/hswebframework/web/crud/web/ResponseMessageWrapper.java

@@ -0,0 +1,55 @@
+package org.hswebframework.web.crud.web;
+
+import org.springframework.core.MethodParameter;
+import org.springframework.core.ReactiveAdapterRegistry;
+import org.springframework.http.codec.HttpMessageWriter;
+import org.springframework.web.reactive.HandlerResult;
+import org.springframework.web.reactive.accept.RequestedContentTypeResolver;
+import org.springframework.web.reactive.result.method.annotation.ResponseBodyResultHandler;
+import org.springframework.web.server.ServerWebExchange;
+import reactor.core.publisher.Mono;
+
+import java.util.List;
+
+public class ResponseMessageWrapper extends ResponseBodyResultHandler {
+    public ResponseMessageWrapper(List<HttpMessageWriter<?>> writers, RequestedContentTypeResolver resolver, ReactiveAdapterRegistry registry) {
+        super(writers, resolver, registry);
+        setOrder(90);
+    }
+
+    private static MethodParameter param;
+
+    static {
+        try {
+            param = new MethodParameter(ResponseMessageWrapper.class
+                    .getDeclaredMethod("methodForParams"), -1);
+        } catch (NoSuchMethodException e) {
+            e.printStackTrace();
+        }
+    }
+
+    private static Mono<ResponseMessage<?>> methodForParams() {
+        return Mono.empty();
+    }
+
+    @Override
+    public boolean supports(HandlerResult result) {
+        boolean isAlreadyResponse = result.getReturnType().resolveGeneric(0) == ResponseMessage.class;
+        boolean isMono = result.getReturnType().resolve() == Mono.class;
+        return isMono && super.supports(result) && !isAlreadyResponse;
+    }
+
+    @Override
+    @SuppressWarnings("all")
+    public Mono<Void> handleResult(ServerWebExchange exchange, HandlerResult result) {
+        Object body = result.getReturnValue();
+        if (body instanceof Mono) {
+            body = ((Mono) body).map(ResponseMessage::ok);
+        }
+        if (body == null) {
+            body = Mono.just(ResponseMessage.ok());
+        }
+        return writeBody(body, param, exchange);
+
+    }
+}

+ 2 - 1
hsweb-commons/hsweb-commons-crud/src/main/resources/META-INF/spring.factories

@@ -2,4 +2,5 @@
 org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
 org.hswebframework.web.crud.configuration.EasyOrmConfiguration,\
 org.hswebframework.web.crud.configuration.JdbcSqlExecutorConfiguration,\
-org.hswebframework.web.crud.configuration.R2dbcSqlExecutorConfiguration
+org.hswebframework.web.crud.configuration.R2dbcSqlExecutorConfiguration,\
+org.hswebframework.web.crud.web.CommonWebFluxConfiguration