JavaSpring.apiUtil.mustache Maven / Gradle / Ivy
package {{apiPackage}};
{{#reactive}}
import org.springframework.core.io.buffer.DefaultDataBufferFactory;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;
{{/reactive}}
{{^reactive}}
import org.springframework.web.context.request.NativeWebRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
{{/reactive}}
public class ApiUtil {
{{^reactive}}
public static void setExampleResponse(NativeWebRequest req, String contentType, String example) {
try {
req.getNativeResponse(HttpServletResponse.class).addHeader("Content-Type", contentType);
req.getNativeResponse(HttpServletResponse.class).getOutputStream().print(example);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
{{/reactive}}
{{#reactive}}
public static Mono getExampleResponse(ServerWebExchange exchange, String example) {
return exchange.getResponse().writeWith(Mono.just(new DefaultDataBufferFactory().wrap(example.getBytes())));
}
{{/reactive}}
}