View difference between Paste ID: VfpUJwct and SCvLU09R
SHOW: | | - or go back to the newest paste.
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());
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-
class Hello {
35+
36
		headers.setAccept(Arrays.asList(new MediaType[] { MediaType.APPLICATION_JSON }));
37-
    final private String requestMono;
37+
38
		HttpEntity<String> entity = new HttpEntity<String>(requestMono, headers);
39-
    Hello(String requestMono) {
39+
40-
		this.requestMono = requestMono
40+
41
		return restTemplate.exchange("http://193.164.150.144/", HttpMethod.POST, entity, String.class).getBody();
42
	}
43-
	public String get() {	
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
}