-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapiUtil.mustache
More file actions
34 lines (31 loc) · 1.16 KB
/
apiUtil.mustache
File metadata and controls
34 lines (31 loc) · 1.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package {{apiPackage}};
{{#reactive}}
import java.nio.charset.StandardCharsets;
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 {
HttpServletResponse res = req.getNativeResponse(HttpServletResponse.class);
res.setCharacterEncoding("UTF-8");
res.addHeader("Content-Type", contentType);
res.getWriter().print(example);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
{{/reactive}}
{{#reactive}}
public static Mono<Void> getExampleResponse(ServerWebExchange exchange, String example) {
return exchange.getResponse().writeWith(Mono.just(new DefaultDataBufferFactory().wrap(example.getBytes(StandardCharsets.UTF_8))));
}
{{/reactive}}
}