content: package cn.sp.chain;
  
  import com.google.gson.Gson;
  import com.google.gson.GsonBuilder;
  import org.springframework.http.MediaType;
  import org.springframework.util.Assert;
  import org.springframework.web.server.ServerWebExchange;
  import reactor.core.publisher.Mono;
  
  
  /**
   * Created by 2YSP on 2020/12/26
   */
  public class ShipResponseUtil {
  
      private static Gson gson = new GsonBuilder().create();
  
      /**
       * @param exchange
       * @param resp
       */
      public static Mono<Void> doResponse(ServerWebExchange exchange, String resp) {
          Assert.notNull(resp, "response object can't be null");
          exchange.getResponse().getHeaders().setContentType(MediaType.APPLICATION_JSON);
          return exchange.getResponse().writeWith(Mono.just(exchange.getResponse()
                  .bufferFactory().wrap(resp.getBytes())));
      }
  
      /**
       *
       * @param exchange
       * @param t
       * @param <T>
       * @return
       */
      public static <T> Mono<Void> doResponse(ServerWebExchange exchange, T t) {
          Assert.notNull(t, "response object can't be null");
          String resp = gson.toJson(t);
          return doResponse(exchange, resp);
      }
  
  }
  