spring boot in the web tier - dave syer

26
Spring Boot for the Web Tier Dave Syer, 2015

Upload: jaxlondonconference

Post on 14-Apr-2017

429 views

Category:

Software


0 download

TRANSCRIPT

Page 1: Spring Boot in the Web Tier - Dave Syer

Spring Boot for the Web TierDave Syer, 2015

Page 2: Spring Boot in the Web Tier - Dave Syer

AuthorDave Syer twitter: [@david_syer](http://twitter.com/david_syer) email: [email protected]

With thanks to: Stephane Nicoll (@snicoll) and Brian Clozel (@bclozel)

Page 3: Spring Boot in the Web Tier - Dave Syer

Presentation OverviewPart 1 ­ Static Content

Part 2 ­ Dynamic Content

Part 3 ­ Embedded Server

Part 4 ­ Other Stacks

Page 4: Spring Boot in the Web Tier - Dave Syer

SamplesSpring Boot @ConfigurationProperties Difference Analyser

https://github.com/dsyer/spring­boot­4tw

1.  Traditional server side rendering

2.  Assets in separate project

3.  Single project, webjars

4.  Single project, build time assembly

5.  Separate service and CORS

Page 5: Spring Boot in the Web Tier - Dave Syer

Static Content ­ Serving FilesCan’t use /src/main/webapp for jar deployments

Put static files from src/main/resources/static

or …/public or …/resources or …/META-INF/resourcesImported "webjars" are automatically mapped

Page 6: Spring Boot in the Web Tier - Dave Syer

Static Content: NPM ToolchainFor serious front end developers the best choice is a Javascript toolchain.

Good community, lots of tools

Package static assets into a jar

And/or build them as part of a very thin back end

Spring Boot CLI makes a great lightweight back end in production or for Java devs

Page 7: Spring Boot in the Web Tier - Dave Syer

Static Content ­ webjarsGreat for Java developers

No compilation or pre­processing

Huge range of Java Script and CSS libraries

Spring MVC support for webjars­locator

Page 8: Spring Boot in the Web Tier - Dave Syer

Static Content ­ wro4jGreat for Java developers

Minification, consolidation, compilation, etc.

Often good enough

Tutorial (with AngularJS): https://spring.io/guides/tutorials/spring­security­and­angular­js/

Page 9: Spring Boot in the Web Tier - Dave Syer

Dynamic Content ­ API BackendJSON is trivial using Spring REST content negotiation

Add HttpMessageConverter beans and Spring Boot will try to do the right thingIt tries to be intelligent about the order

Add a HttpMessageConverters bean if you need more controlSpring Data REST is very popular for CRUD + query

Page 10: Spring Boot in the Web Tier - Dave Syer

Dynamic Content ­ API DocumentationSpring Restdocs: http://spring.io/projects/spring­restdocs

Generate documentation driven by test cases

Use (e.g.) asciidoctor to format and package it

Page 11: Spring Boot in the Web Tier - Dave Syer

Dynamic Content ­ Templating SupportThymeleaf

Mustache

Groovy Template Language

Freemarker

Velocity (deprecated in Spring 4.3)

JSP (not recommended)

Page 12: Spring Boot in the Web Tier - Dave Syer

Hot ReloadIDEs support reloading static resources

Also small changes to Java classes

No agent: Spring Boot devtools

Agent: spring­loaded (Grails, JHipster), JRebel

Live reload (browser plugin)

Page 13: Spring Boot in the Web Tier - Dave Syer

Embedded ServerWhen using WARs a ServletContainerInitializer creates the Spring ApplicationContext

When running embedded the ApplicationContext creates the Server

Expects a single EmbeddedServletContainerFactory bean

Odd dance for WebApplicationContext.getServletContext() and ServletConfigAware

Page 14: Spring Boot in the Web Tier - Dave Syer

Embedded Server ­ InitializationThe following beans are used to configure the server:

Servlet

Filter

ServletRequestListener

ServletRequestAttributeListener

HttpSessionAttributeListener

HttpSessionListener

ServletContextListener

Page 15: Spring Boot in the Web Tier - Dave Syer

Embedded Server ­ InitializationFor more control use

ServletRegistrationBean

FilterRegistrationBean

ServletListenerRegistrationBean

@Beanpublic ServletRegistrationBean myServlet() { ServletRegistrationBean bean = new ServletRegistrationBean(new MyServlet(), "/mine"); bean.setAsyncSupported(false); bean.setInitParameters(Collections.singletonMap("debug", "true")); return bean;}

@Beanpublic FilterRegistrationBean myFilter() { return new FilterRegistrationBean(new MyFilter(), myServlet());}

Page 16: Spring Boot in the Web Tier - Dave Syer

Embedded Server ­ InitializationBy design the following are not called with embedded servers:

javax.servlet.ServletContainerInitializer

org.springframework.web.WebApplicationInitializer

Use o.s.boot.context.embedded.ServletContextInitializer

/** * Configure the given {@link ServletContext} with any servlets, filters, listeners * context-params and attributes necessary for initialization. * @param servletContext the {@code ServletContext} to initialize * @throws ServletException if any call against the given {@code ServletContext} * throws a {@code ServletException} */ void onStartup(ServletContext servletContext) throws ServletException;

Page 17: Spring Boot in the Web Tier - Dave Syer

Embedded Server ­ CustomizationUse ServerProperties (e.g. server.port=8080)

EmbeddedServletContainerCustomizer

Customize common things (e.g. the port, error-pages, context-path)Tomcat Specific

TomcatConnectorCustomizer

TomcatContextCustomizerJetty Specific

JettyServerCustomizerUndertow Specific

UndertowBuilderCustomizer

Page 18: Spring Boot in the Web Tier - Dave Syer

Embedded Server ­ Tomcat Behind ProxyRunning behind nginx or Apache HTTPD is a common option

Especially useful with SSL termination

Real IP and SSL information is passed in headers

server.tomcat.protocol-header=x-forwarded-protoserver.tomcat.remote-ip-header=x-forwarded-for

Spring Boot 1.3.0 supports other containers in a similar way

Page 19: Spring Boot in the Web Tier - Dave Syer

Other StacksJAX­RS: Jersey 1.x, Jersey 2.x, CXF (allegedly works)

Netty and NIO: Ratpack dsyer/spring­boot­ratpack

Servlet 2.5 scratches/spring­boot­legacy

Vaadin peholmst/vaadin4spring

Page 20: Spring Boot in the Web Tier - Dave Syer

RatpackOriginally inspired by Sinatra, but now pretty much diverged. Provides a nice programming model on top of Netty (taking advantage of non­blocking IOwhere possible).

2 approaches:

Ratpack embeds Spring (and uses it as a Registry), supported natively in RatpackSpring embeds Ratpack (and uses it as an HTTP listener) = spring­boot­ratpack

Page 21: Spring Boot in the Web Tier - Dave Syer

Spring Boot embedding RatpackTrivial example (single Handler):

@Beanpublic Handler handler() { return (context) -> { context.render("Hello World"); };}

Page 22: Spring Boot in the Web Tier - Dave Syer

Spring Boot embedding RatpackMore interesting example (Action<Chain> registers Handlers):

@Beanpublic Handler hello() { return (context) -> { context.render("Hello World"); };}

@Beanpublic Action<Chain> handlers() { return (chain) -> { chain.get(hello()); };}

Page 23: Spring Boot in the Web Tier - Dave Syer

Spring Boot Ratpack DSLA valid Ratpack Groovy application:

ratpack { handlers { get { render "Hello World" } }}

launched with Spring Boot:

$ spring run app.groovy

Page 24: Spring Boot in the Web Tier - Dave Syer

SamplesSpring Boot @ConfigurationProperties Difference Analyser

https://github.com/dsyer/spring­boot­4tw

1.  Traditional server side rendering: spring­boot­4tw

2.  Assets in separate project: spring­boot­4tw­webjs, spring­boot­4tw­client

3.  Single project, webjars: spring­boot­4tw­webjars

4.  Single project, build time assembly: spring­boot­4tw­wro4j

5.  Separate service and CORS: spring­boot­4tw­versions­provider

Page 25: Spring Boot in the Web Tier - Dave Syer

Questions?http://projects.spring.io/spring­boot/

http://projects.spring.io/spring­restdocs/

https://github.com/dsyer/spring­boot­4tw

https://github.com/SpringOne2GX­2014/spring­boot­for­the­web­tier

Page 26: Spring Boot in the Web Tier - Dave Syer

← →1 / 25Go to Slide:    Go