Skip to content

Instantly share code, notes, and snippets.

@tyama
Forked from danveloper/MainController.java
Last active August 29, 2015 14:19
Show Gist options
  • Save tyama/72132c38264cede838aa to your computer and use it in GitHub Desktop.
Save tyama/72132c38264cede838aa to your computer and use it in GitHub Desktop.
package app;
import org.springframework.beans.MutablePropertyValues;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.GenericBeanDefinition;
import org.springframework.boot.SpringApplication;
import org.springframework.http.MediaType;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.http.server.ServletServerHttpResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.SimpleUrlHandlerMapping;
import org.springframework.web.servlet.mvc.Controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;
public class MainController implements Controller {
private static final Map<String, String> RESPONSE = new HashMap<String, String>() {{
put("response", "Hello World!");
}};
static final Class[] BOOT_CLASSES = new Class[]{
MainController.class,
org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration.class,
org.springframework.boot.autoconfigure.MessageSourceAutoConfiguration.class,
org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration.class,
org.springframework.boot.autoconfigure.web.DispatcherServletAutoConfiguration.class,
org.springframework.boot.autoconfigure.web.EmbeddedServletContainerAutoConfiguration.class,
org.springframework.boot.autoconfigure.web.HttpMessageConvertersAutoConfiguration.class,
org.springframework.boot.autoconfigure.web.MultipartAutoConfiguration.class,
org.springframework.boot.autoconfigure.web.ServerPropertiesAutoConfiguration.class,
org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration.class,
};
public static void main(String[] args) throws Exception {
SpringApplication app = new SpringApplication(BOOT_CLASSES);
app.addInitializers(applicationContext -> {
GenericBeanDefinition definition = new GenericBeanDefinition();
MutablePropertyValues values = new MutablePropertyValues();
values.add("urlMap", new HashMap<String, String>() {{
put("/main*", "mainController");
}});
values.add("order", 0);
definition.setPropertyValues(values);
definition.setBeanClass(SimpleUrlHandlerMapping.class);
((BeanDefinitionRegistry) applicationContext.getBeanFactory())
.registerBeanDefinition("mainUrlMapping", definition);
});
app.run();
}
@Override
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
new MappingJackson2HttpMessageConverter().write(RESPONSE, MediaType.APPLICATION_JSON, new ServletServerHttpResponse(response));
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment