Simply speaking:
JAX-RS, namely Java API for RESTful Service, is from JSR 311 (obsolete) and JSR 339 (download here).
Jersey is an reference implementation of JAX-RS.
Spring boot sure can implement REST service without Jersey by using the controller way (@RestConroller). Also, jersey can be chosen for exposing the RESTful services.
This is a hello world level example of using Jersey with spring boot to provide RESTful services.
0. what you need
In this demo, following are used:
- java 8
- maven 3.2
- spring boot 1.4.0.RELEASE
Jersey is included in spring boot release.
1. Maven pom.xml
Only one dependency is needed.
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.shengwang.demo</groupId> <artifactId>rest-versioning</artifactId> <version>1.0</version> <packaging>jar</packaging> <name>rest-versioning</name> <description>Demo project for spring boot jersey rest</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.0.RELEASE</version> <relativePath/> </parent> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <!-- only dependency needed --> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jersey</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
2. Java classes
In this hello world example, there are 5 classes totally.
- Main class
- POJO model class
- Service
- Jersey configuration
- Endpoint
They will be shown one by one. The Jersey configuration and Endpoint class are more interesting, but for the completion for the demo, all classes are listed below.
2.1 Main class
This class is just a trival main class from spring boot.
package com.shengwang.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class SpringBootJerseyApplication { public static void main(String[] args) { SpringApplication.run(SpringBootJerseyApplication.class, args); } }
2.2. POJO model
For demo usage. A model class is created. It’s just a POJO.
package com.shengwang.demo.model; public class User { private String name; private int age; public User(String name, int age) { this.name = name; this.age = age; } // setter, getter ignored }
2.3 Service class
A demo class just return a User object by a userId.
package com.shengwang.demo.service; import com.shengwang.demo.model.User; import org.springframework.stereotype.Service; import javax.annotation.PostConstruct; import java.util.HashMap; import java.util.Map; @Service public class UserService { private Map<String,User> users; @PostConstruct private void loadUser() { users = new HashMap<>(); users.put("1",new User("Tom",20)); users.put("2",new User("Jerry",19)); } public User findById(String id) { return users.get(id); } }
2.4 Jersey configuration
package com.shengwang.demo; import com.fasterxml.jackson.databind.ObjectMapper; import org.glassfish.jersey.server.ResourceConfig; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import javax.ws.rs.ApplicationPath; import javax.ws.rs.ext.ContextResolver; import javax.ws.rs.ext.Provider; @Component @ApplicationPath("/v1") public class JerseyConfig extends ResourceConfig { @Autowired public JerseyConfig(ObjectMapper objectMapper) { // register endpoints packages("com.shengwang.demo"); // register jackson for json register(new ObjectMapperContextResolver(objectMapper)); } @Provider public static class ObjectMapperContextResolver implements ContextResolver<ObjectMapper> { private final ObjectMapper mapper; public ObjectMapperContextResolver(ObjectMapper mapper) { this.mapper = mapper; } @Override public ObjectMapper getContext(Class<?> type) { return mapper; } } }
The JerseyConfig extends from jersey ResourceConfig, just register the endpoints and jackson for json.
2.5 Endpoint
Just like the spring controller, the jersey endpoint provides the URL mapping.
package com.shengwang.demo.endpoint; import com.shengwang.demo.model.User; import com.shengwang.demo.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; @Component @Path("/users") public class DemoEndpoint { @Autowired private UserService userService; @GET @Path("/{id}") @Produces(MediaType.APPLICATION_JSON) public User getEventVersion1(@PathParam("id") String id) { return userService.findById(id); } }
Now everything is ready, the whole project looks like below.
3. Run it
Start the spring boot application from your IDE, and access the url from a brower. Here is the json result you get: