1

ive been tasked with a total refactor of legacy code. It's a simple webservice, just an http request, then business logic with possibly a few database calls and a few other microservice calls, then a json response. I am being pushed not to use spring boot because no one else around me has used it before, and I was told jersey does everything spring boot does. I've never used jersey so im trying to find out how to do things that spring boot makes simple (ie repository layer with spring-data, caching, spring-consul, spring-zuul, spring-actuator, spring-circuit-breaker) It looks like jersey does do an analog of spring-security, bean validation, and easy insertion of servlet filters, but not everything spring-boot does. Is there an easy way to wire in a JPA type repository in jersey? I cant find it in the docs at https://jersey.java.net/documentation/latest/index.html.

Dustin
  • 322
  • 3
  • 10
  • 1
    Jersey is the reference implementation of JAX-RS (http://docs.oracle.com/javaee/6/tutorial/doc/giepu.html) and as such, doesn't concern itself with things like your actual repository layer. Your colleagues are misinformed when they say Jersey covers the same surface area as all of the Spring Framework libraries. – Thorn G Oct 25 '16 at 20:57
  • thank you, thats kind of what i was hoping to hear – Dustin Oct 25 '16 at 21:00

1 Answers1

2

I think about it this way. There are different layers in your application. You have a service layer, and you have a "REST layer". You access the Spring repositories with the service layer. Then you have the REST layer. With Spring, you have Spring MVC which is its web layer implementation, that you can also use as REST services. There is also Jersey, which is completely independent of Spring, which is a another REST layer options.

That be said, when using Spring MVC as the REST layer, adding the service layer with Spring data is seamless. But Jersey also has integration with Spring, that allows us to use Spring at the service layer inside our Jersey REST services. You check out this post which has some links to example of how this can be done (no hacking, this is supported out of the box). Using this approach, you can just injector your Spring data repositories into your Jersey resource class

interface PetsRepository extends JpaRepository<Pets, Long> {}

@Path("/pets")
class PetsResource {
  @Autowired
  private PetsRepository repo;
}

Now lets talk about Spring Boot. Spring Boot is just a bootstrapping framework. What it does is allow you to easily bootstrap an application without all the boilerplate configuration you would need without it. When your using Spring Boot for your REST services, you're not actually using Spring Boot itself as the REST service engine. You are only using it to bootstrap Spring MVC and maybe your Spring Data. But Spring MVC is the actual REST service engine.

Now like I said before, Jersey has support for integrating Spring into into it (for the service layer). Because of this support, Spring Boot has also provided a bootstrap configuration to integrate this support seamlessly. So instead of using the manual configuration that you would see in one of the examples linked to above, Spring Boot handles this configuration for us. So we can use Jersey as the REST layer, and Spring beans as the service layer. Check out the links below

See also:

Graham
  • 7,431
  • 18
  • 59
  • 84
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
  • thank you!! i didnt realize that spring mvc was spring boot's rest layer, that makes a lot of sense. I wouldnt mind using jersey as the rest layer in my spring boot project, like your links show its not that much work to swap out spring mvc, but im being told not to use spring boot at all. how would you answer someone who says spring boot brings in too many dependancies? and that microservices should be small and not have any jars they dont need? – Dustin Oct 26 '16 at 13:04
  • 1
    I'd probably argue that the "micro" in microservices does not mean the size of the application. As far as dependencies, I'd argue that why would you want to have to implement something that is already implemented and battle tested. I mean this in regards to any of Spring's services it offers in its eco-system (Spring data included). By adding Spring support in the service layer you have access to these services. As far as the difference between using just Spring (as in the examples I linked to) and using Spring Boot, I'm not sure how much else Spring Boot imports aside from core Spring.... – Paul Samsotha Oct 26 '16 at 13:14
  • 1
    ..dependencies. You might want to create a project without Spring Boot (from one of the example I linked to), and then one with Spring Boot, using the example in the See also documentation link. And see how much of a difference it makes (as far as extra dependencies pulled in by Spring Boot as opposed to just using core spring) – Paul Samsotha Oct 26 '16 at 13:16
  • 1
    Personally, I'd say that if size is the only problem, I'd probably argue that the benefits completely outweigh that issue. – Paul Samsotha Oct 26 '16 at 13:17