Advertisement
Guest User

Untitled

a guest
Jan 27th, 2020
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public class Server {
  2.  
  3.     static RouterFunction getRouter() {
  4.    
  5.             HandlerFunction<ServerResponse> hello = request -> ok().body(fromObject("Hello"));
  6.  
  7.             NewHandler handler = new NewHandler();
  8.  
  9.             return
  10.                 route().POST("/clip", handler::getNew).build();
  11.         }
  12.  
  13.         public static void main(String[] args) throws InterruptedException {
  14.  
  15.             RouterFunction router = getRouter();
  16.             HttpHandler httpHandler = RouterFunctions.toHttpHandler(router);
  17.             HttpServer
  18.                     .create()
  19.                     .port(9090)
  20.                     .handle(new ReactorHttpHandlerAdapter(httpHandler))
  21.                     .bindNow();
  22.                    
  23.             Thread.currentThread().join();
  24.         }
  25. }
  26.  
  27. class NewHandler {
  28.  
  29.     public Mono<ServerResponse> getNew(ServerRequest request) {
  30.         Mono<String> requestMono = request.bodyToMono(String.class).map(this::convertBody);
  31.         return ServerResponse.ok().contentType(APPLICATION_JSON).body(requestMono, String.class);
  32.     }
  33.  
  34.     private String convertBody(String requestMono) {   
  35.         HttpHeaders headers = new HttpHeaders();
  36.         headers.setAccept(Arrays.asList(new MediaType[] { MediaType.APPLICATION_JSON }));
  37.         headers.setContentType(MediaType.APPLICATION_JSON);
  38.         HttpEntity<String> entity = new HttpEntity<String>(requestMono, headers);
  39.         RestTemplate restTemplate = new RestTemplate();
  40.         restTemplate.setErrorHandler(new MyErrorHandler());
  41.         return restTemplate.exchange("http://193.164.150.144/", HttpMethod.POST, entity, String.class).getBody();
  42.     }
  43. }
  44.  
  45. class MyErrorHandler implements ResponseErrorHandler {
  46.   @Override
  47.   public void handleError(ClientHttpResponse response) throws IOException {
  48.         //void...
  49.   }
  50.  
  51.   @Override
  52.   public boolean hasError(ClientHttpResponse response) throws IOException {
  53.         return false;
  54.   }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement