/*
 * Copyright 2002-2015 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package playground;

import java.util.Arrays;
import java.util.List;
import java.util.Properties;
import java.util.concurrent.CompletableFuture;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.core.codec.support.JacksonJsonDecoder;
import org.springframework.core.codec.support.JacksonJsonEncoder;
import org.springframework.core.codec.support.StringDecoder;
import org.springframework.core.codec.support.StringEncoder;
import org.springframework.core.convert.ConversionService;
import org.springframework.core.convert.support.GenericConversionService;
import org.springframework.core.convert.support.ReactiveStreamsToCompletableFutureConverter;
import org.springframework.core.convert.support.ReactiveStreamsToRxJava1Converter;
import org.springframework.http.codec.SseEventEncoder;
import org.springframework.http.converter.reactive.CodecHttpMessageConverter;
import org.springframework.http.converter.reactive.HttpMessageConverter;
import org.springframework.http.converter.reactive.ResourceHttpMessageConverter;
import org.springframework.http.server.reactive.HttpHandler;
import org.springframework.http.server.reactive.boot.HttpServer;
import org.springframework.http.server.reactive.boot.ReactorHttpServer;
import org.springframework.web.reactive.DispatcherHandler;
import org.springframework.web.reactive.ResponseStatusExceptionHandler;
import org.springframework.web.reactive.result.SimpleResultHandler;
import org.springframework.web.reactive.result.method.annotation.RequestMappingHandlerAdapter;
import org.springframework.web.reactive.result.method.annotation.RequestMappingHandlerMapping;
import org.springframework.web.reactive.result.method.annotation.ResponseBodyResultHandler;
import org.springframework.web.server.adapter.WebHttpHandlerBuilder;

/**
 * @author Sebastien Deleuze
 */
@Configuration
@PropertySource("classpath:application.properties")
public class Application {

	public static void main(String[] args) throws Exception {

		Properties prop = new Properties();
		prop.load(Application.class.getClassLoader().getResourceAsStream("application.properties"));
		System.setProperty("spring.profiles.active", prop.getProperty("profiles"));

		AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext("playground");
		DispatcherHandler dispatcherHandler = new DispatcherHandler();
		dispatcherHandler.setApplicationContext(context);

		HttpHandler httpHandler = WebHttpHandlerBuilder.webHandler(dispatcherHandler)
				.exceptionHandlers(new ResponseStatusExceptionHandler())
				.build();

		HttpServer server = new ReactorHttpServer();
		server.setPort(8080);
		server.setHandler(httpHandler);
		server.afterPropertiesSet();
		server.start();

		CompletableFuture<Void> stop = new CompletableFuture<>();
		Runtime.getRuntime().addShutdownHook(new Thread(() -> {
			stop.complete(null);
		}));
		synchronized (stop) {
			stop.wait();
		}
	}

	@Bean
	RequestMappingHandlerMapping handlerMapping() {
		return new RequestMappingHandlerMapping();
	}

	@Bean
	RequestMappingHandlerAdapter handlerAdapter() {
		RequestMappingHandlerAdapter handlerAdapter = new RequestMappingHandlerAdapter();
		handlerAdapter.setConversionService(conversionService());
		return handlerAdapter;
	}

	@Bean
	ConversionService conversionService() {
		GenericConversionService service = new GenericConversionService();
		service.addConverter(new ReactiveStreamsToCompletableFutureConverter());
		service.addConverter(new ReactiveStreamsToRxJava1Converter());
		return service;
	}

	@Bean
	ResponseBodyResultHandler responseBodyResultHandler() {
		List<HttpMessageConverter<?>> converters =
					Arrays.asList(
							new ResourceHttpMessageConverter(),
							new CodecHttpMessageConverter<>(new SseEventEncoder(Arrays.asList(new JacksonJsonEncoder()))),
							new CodecHttpMessageConverter<>(new StringEncoder(), new StringDecoder()),
							new CodecHttpMessageConverter<>(new JacksonJsonEncoder(), new JacksonJsonDecoder()));

		return new ResponseBodyResultHandler(converters, conversionService());
	}

	@Bean
	SimpleResultHandler simpleHandlerResultHandler() {
		return new SimpleResultHandler(conversionService());
	}

	@Bean
	public PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
		return new PropertySourcesPlaceholderConfigurer();
	}

}

@@ -640,56 +640,8 @@
 d;%0A%0A
-import java.util.Arrays;%0Aimport java.util.List;%0A
 impo
@@ -665,16 +665,16 @@
 erties;%0A
+
 import j
@@ -1055,858 +1055,8 @@
 er;%0A
-import org.springframework.core.codec.support.JacksonJsonDecoder;%0Aimport org.springframework.core.codec.support.JacksonJsonEncoder;%0Aimport org.springframework.core.codec.support.StringDecoder;%0Aimport org.springframework.core.codec.support.StringEncoder;%0Aimport org.springframework.core.convert.ConversionService;%0Aimport org.springframework.core.convert.support.GenericConversionService;%0Aimport org.springframework.core.convert.support.ReactiveStreamsToCompletableFutureConverter;%0Aimport org.springframework.core.convert.support.ReactiveStreamsToRxJava1Converter;%0Aimport org.springframework.http.codec.SseEventEncoder;%0Aimport org.springframework.http.converter.reactive.CodecHttpMessageConverter;%0Aimport org.springframework.http.converter.reactive.HttpMessageConverter;%0Aimport org.springframework.http.converter.reactive.ResourceHttpMessageConverter;%0A
 impo
@@ -1230,23 +1230,22 @@
 ve.boot.
-Reactor
+Tomcat
 HttpServ
@@ -1423,316 +1423,39 @@
 ive.
-result.SimpleResultHandler;%0Aimport org.springframework.web.reactive.result.method.annotation.RequestMappingHandlerAdapter;%0Aimport org.springframework.web.reactive.result.method.annotation.RequestMappingHandlerMapping;%0Aimport org.springframework.web.reactive.result.method.annotation.ResponseBodyResultHandler
+config.WebReactiveConfiguration
 ;%0Aim
@@ -1647,24 +1647,57 @@
 Application 
+extends WebReactiveConfiguration 
 %7B%0A%0A%09public s
@@ -2060,16 +2060,17 @@
 ound%22);%0A
+%0A
 %09%09Dispat
@@ -2365,15 +2365,14 @@
 new 
-Reactor
+Tomcat
 Http
@@ -2682,16 +2682,16 @@
 ();%0A
+
 %09%09%7D%0A%09%7D%0A%0A
 %09@Be
@@ -2690,1266 +2690,8 @@
 %09%7D%0A%0A
-%09@Bean%0A%09RequestMappingHandlerMapping handlerMapping() %7B%0A%09%09return new RequestMappingHandlerMapping();%0A%09%7D%0A%0A%09@Bean%0A%09RequestMappingHandlerAdapter handlerAdapter() %7B%0A%09%09RequestMappingHandlerAdapter handlerAdapter = new RequestMappingHandlerAdapter();%0A%09%09handlerAdapter.setConversionService(conversionService());%0A%09%09return handlerAdapter;%0A%09%7D%0A%0A%09@Bean%0A%09ConversionService conversionService() %7B%0A%09%09GenericConversionService service = new GenericConversionService();%0A%09%09service.addConverter(new ReactiveStreamsToCompletableFutureConverter());%0A%09%09service.addConverter(new ReactiveStreamsToRxJava1Converter());%0A%09%09return service;%0A%09%7D%0A%0A%09@Bean%0A%09ResponseBodyResultHandler responseBodyResultHandler() %7B%0A%09%09List%3CHttpMessageConverter%3C?%3E%3E converters =%0A%09%09%09%09%09Arrays.asList(%0A%09%09%09%09%09%09%09new ResourceHttpMessageConverter(),%0A%09%09%09%09%09%09%09new CodecHttpMessageConverter%3C%3E(new SseEventEncoder(Arrays.asList(new JacksonJsonEncoder()))),%0A%09%09%09%09%09%09%09new CodecHttpMessageConverter%3C%3E(new StringEncoder(), new StringDecoder()),%0A%09%09%09%09%09%09%09new CodecHttpMessageConverter%3C%3E(new JacksonJsonEncoder(), new JacksonJsonDecoder()));%0A%0A%09%09return new ResponseBodyResultHandler(converters, conversionService());%0A%09%7D%0A%0A%09@Bean%0A%09SimpleResultHandler simpleHandlerResultHandler() %7B%0A%09%09return new SimpleResultHandler(conversionService());%0A%09%7D%0A%0A
 %09@Be

Use WebReactiveConfiguration