Guest User

Untitled

a guest
Jan 27th, 2020
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.96 KB | None | 0 0
  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(body -> new Hello(body).get());
  31.         return ServerResponse.ok().contentType(APPLICATION_JSON).body(requestMono, String.class);
  32.     }
  33. }
  34.  
  35. class Hello {
  36.  
  37.     final private String requestMono;
  38.  
  39.     Hello(String requestMono) {
  40.         this.requestMono = requestMono
  41.     }
  42.  
  43.     public String get() {  
  44.         HttpHeaders headers = new HttpHeaders();
  45.         headers.setAccept(Arrays.asList(new MediaType[] { MediaType.APPLICATION_JSON }));
  46.         headers.setContentType(MediaType.APPLICATION_JSON);
  47.         HttpEntity<String> entity = new HttpEntity<String>(requestMono, headers);
  48.         RestTemplate restTemplate = new RestTemplate();
  49.         restTemplate.setErrorHandler(new MyErrorHandler());
  50.         return restTemplate.exchange("http://193.164.150.144/", HttpMethod.POST, entity, String.class).getBody();
  51.     }
  52. }
  53.  
  54. class MyErrorHandler implements ResponseErrorHandler {
  55.   @Override
  56.   public void handleError(ClientHttpResponse response) throws IOException {
  57.         //void...
  58.   }
  59.  
  60.   @Override
  61.   public boolean hasError(ClientHttpResponse response) throws IOException {
  62.         return false;
  63.   }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment