Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Server {
- static RouterFunction getRouter() {
- HandlerFunction<ServerResponse> hello = request -> ok().body(fromObject("Hello"));
- NewHandler handler = new NewHandler();
- return
- route().POST("/clip", handler::getNew).build();
- }
- public static void main(String[] args) throws InterruptedException {
- RouterFunction router = getRouter();
- HttpHandler httpHandler = RouterFunctions.toHttpHandler(router);
- HttpServer
- .create()
- .port(9090)
- .handle(new ReactorHttpHandlerAdapter(httpHandler))
- .bindNow();
- Thread.currentThread().join();
- }
- }
- class NewHandler {
- public Mono<ServerResponse> getNew(ServerRequest request) {
- Mono<String> requestMono = request.bodyToMono(String.class).map(this::convertBody);
- return ServerResponse.ok().contentType(APPLICATION_JSON).body(requestMono, String.class);
- }
- private String convertBody(String requestMono) {
- HttpHeaders headers = new HttpHeaders();
- headers.setAccept(Arrays.asList(new MediaType[] { MediaType.APPLICATION_JSON }));
- headers.setContentType(MediaType.APPLICATION_JSON);
- HttpEntity<String> entity = new HttpEntity<String>(requestMono, headers);
- RestTemplate restTemplate = new RestTemplate();
- restTemplate.setErrorHandler(new MyErrorHandler());
- return restTemplate.exchange("http://193.164.150.144/", HttpMethod.POST, entity, String.class).getBody();
- }
- }
- class MyErrorHandler implements ResponseErrorHandler {
- @Override
- public void handleError(ClientHttpResponse response) throws IOException {
- //void...
- }
- @Override
- public boolean hasError(ClientHttpResponse response) throws IOException {
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement