This guide is for someone interesting in quickly building a micro-service using the Spring Boot framework. Works great if you have prior JAX-RS / Jersey experience
Step-by-step guide
Add the steps involved:
- Create a Maven Project
- Final Structure
- Pom ( Attached here )
- Final Structure
- Include Spring Boot Dependencies in your pom
- Parent Project
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.3.1.RELEASE</version> </parent>
- Dependencies
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency><dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency><dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jersey</artifactId>
</dependency>
</dependencies>
- Parent Project
- Create the Java main class – this is entry point with the @SpringBootApplication annotation
- App.java
@SpringBootApplication public class App { public static void main( String[] args ) { SpringApplication.run(App.class, args); } }
- App.java
- Create the API Controller class
- Add Spring MVC methods for REST in the API controller class
- SimpleAPIController.java with SpringMVC only
@Component @RestController @Path("/api") public class SimpleAPIController { @RequestMapping(value = "/springmvc", produces = "application/json") public Map<String, Object> springMvc() { Map<String, Object> data = new HashMap<String, Object>(); data.put("message", "Spring MVC Implementation of API is available"); return data; } }
- SimpleAPIController.java with SpringMVC only
- (Alternate/Additional) Add Jersery methods for REST in the API controller class
- SimpleAPIController.java with Jersey (Note: You need the spring boot jersey dependency in your pom)
@Component @RestController @Path("/api") public class SimpleAPIController { @GET @Produces({ "application/json" }) public Response getMessage() { Map<String, Object> data = new HashMap<String, Object>(); data.put("message", "Jersey Implementation of API is available"); Response response = Response.ok().entity(data).build(); return response; } }
- SimpleAPIController.java with Jersey (Note: You need the spring boot jersey dependency in your pom)
- (Alternate/Additional) Create a JerseyConfig class
- JerseryConfig.java – required for jersey rest implementation
@Configuration public class JerseyConfig extends ResourceConfig { public JerseyConfig() { register(SimpleAPIController.class); } }
- JerseryConfig.java – required for jersey rest implementation
- Add Spring MVC methods for REST in the API controller class
- Build the Maven project
- Add the Spring boot maven build plugin
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
- Add the Spring boot maven build plugin
- Execute the spring boot application
- Run in Eclipse as Java Application
- Run as a standalone Java Jar
java -jar target/SimpleService-0.0.1-SNAPSHOT.jar
-
Specify a different port and run as a standalone Java Jar
java -Dserver.port=8090 -jar target/SimpleService-0.0.1-SNAPSHOT.jar
-
Running in a docker container
- See: https://spring.io/guides/gs/spring-boot-docker/
- Docker file
FROM java:8 VOLUME /tmp ADD SimpleService-0.0.1-SNAPSHOT.jar app.jar RUN bash -c 'touch /app.jar' ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
- Run in Eclipse as Java Application
The official how-to is here https://spring.io/guides/gs/rest-service/#use-maven and a great blog post is herehttp://blog.codeleak.pl/2015/01/getting-started-with-jersey-and-spring.html |