Skip to content

Instantly share code, notes, and snippets.

@danveloper
Created April 13, 2015 20:38
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save danveloper/6f5900af9c89e57fc884 to your computer and use it in GitHub Desktop.
Save danveloper/6f5900af9c89e57fc884 to your computer and use it in GitHub Desktop.
Spring Boot Hello World JSON No Annotations
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;
}
}
@danveloper
Copy link
Author

curl localhost:8080/main

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment