Jersey with Spring 4 Dependency Injection - java

I am to use Dependency Injection of Spring framework version 4. I have seen that the Jersey has its DI with plugin
<dependency>
<groupId>org.glassfish.jersey.ext</groupId>
<artifactId>jersey-spring3</artifactId>
<version>2.12</version>
</dependency>
Is Jersey's DI of Spring recommended or is there special reason to use it? What if Spring 4 DI is used independently ?
Also please let me know any step by step learning source to integration Spring DI with Jersey ?

The jersey-spring3 extension is not a stand-alone Dependency Injection feature, it's just an extension which makes Jersey aware of Spring's managed beans.
From Jersey - Spring DI:
Jersey provides an extension to support Spring DI. This enables Jersey to use Spring beans as JAX-RS components (e.g. resources and providers) and also allows Spring to inject into Jersey managed components.
...
The above module does not add any transitive dependency to Spring modules, so you will need to add Spring 3 dependencies explicitly into your dependency list.
So if you want to use Jersey with Spring you need jersey-spring3 and all the Spring dependencies you normally use.
By the way, the jersey-spring3 extension is compiled against Spring 3, but should work with Spring 4. See Using Jersey-spring with Spring 4.0 for reference.

You should add jersey-spring3.jar first like the document in jersey website.
For this step by step learning source to integration Spring DI with Jersey, you can do like this when you start up you application debug the application.
Find ServletContainer.class and set a breakpoint in init() function, as this you can find this step by step.

Related

Integrating Camunda webapps in spring framework

I setup Camunda in my Spring 3 project (Tomcat server) using this guide. I embedded the workflow engine in my project.
However, I cannot access the cockpit when I go to the url http://localhost:8080/camunda/app/. I get a 404 error.
I see that there is a dependency to be added in case of Spring boot according to this guide
But I see no such dependencies available for Spring. Do we not get access to webapps while integrating Camunda with Spring?
Also asked this question in the camunda form: https://forum.camunda.org/t/integrating-camunda-webapps-in-spring-framework/27661
You'll need the following dependency.
<dependency>
<groupId>org.camunda.bpm.webapp</groupId>
<artifactId>camunda-webapp-webjar</artifactId>
</dependency>
Then ensure you have the required configurations. Refer the spring boot auto configuration set up here and the web app initialiser here.

Why does the addition of a dependency in Maven trigger functionality?

I have a simple question: I'm just getting started with Open API 3. For this purpose I have added the following dependency in Maven.
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-ui</artifactId>
<version>1.2.30</version>
</dependency>
With the addition of this dependency, can I access the service via localhost:8082/v3/api-docs without having previously set anything and called a function of the dependency? How can this happen? What is the concept behind this ?
Adding the OpenAPI dependency in your Maven pom.xml just adds the librar(ies) to your project. That's all.
If this were a "traditional" project (like a JSP web app, for example), you'd have to write the code to create the web service (e.g. "localhost:8082/v3/api-docs").
But it sounds like your project might be Spring Boot:
https://developer.ibm.com/technologies/java/tutorials/j-spring-boot-basics-perry/
If you let it, Spring Boot will use its #EnableAutoConfiguration
annotation to automatically configure your application.
Auto-configuration is based on the JARS in your classpath and how
you’ve defined your beans:
Spring Boot uses the JARs you have specified to be present in the CLASSPATH to form an opinion about how to configure certain automatic
behavior. For example, if you have the H2 database JAR in your
classpath and have configured no other DataSource beans, then your
application will be automatically configured with an in-memory
database.
Spring Boot uses the way you define beans to determine how to automatically configure itself. For example, if you annotate your JPA
beans with #Entity, then Spring Boot will automatically configure JPA
such that you do not need a persistence.xml file.
It is called convention over configuration.
Wiki link https://en.wikipedia.org/wiki/Convention_over_configuration

how handle Crosscutting concerns in jersy jaxrs

My problem is when we use Spring boot like framework, we can easily handle AOP. but how we handle AOP with jersy jaxrs project
Spring Boot is just a bootstrapping framework. For REST we can use Spring Boot to bootstrap Spring MVC or Jersey fully integrated with Spring. If you choose the latter route, then you can use the Spring AOP with Jersey. All you need to do is make your Jersey resources Spring #Components to able to intercept them. See an official example of Spring Boot and Jersey.
If you don't want to use Spring Boot to bootstrap your Jersey app, then you can still integrate Spring with Jersey. Remember the AOP is not tied to Spring Boot, it is tied to the Spring Framework, which are different things. You can see an example of Jersey with Spring (without Spring Boot) here.
If you don't want to involve Spring at all, then Jersey has a DI framework, HK2, which has it's own AOP. You can see a full example here

Using Jersey and Spring in a Project

I want to create a REST web service using Jersey. I also want to use Spring in the project. Now, my questions is the following:
I don't see any reason for integrating these 2 together in my application. So, I should be able to use Spring for bean management and Jersey for creating the web service. Am I correct, or Spring and Jersey somehow have to be integrated.
I see that there is a jersey-spring maven project, and so, I assume that this is for the purpose of integrating jersey and spring together. My question here is do I get any benefit of using this integrated form rather than simply use Jersey and Spring separately each for its own functionality?
Thanks,
Cyrus
You can absolutely combine the two projects. However, I would encourage you to look at Spring-MVC for doing REST as it is very powerful and easy to use. If memory serves, the jersey-spring project was helpful in integration of JAXB and other touch points. Again, this is all built into Spring. And if you use Spring-Boot it is amazingly simple to get running.
The jersey-spring project provides integration between Jersey and Spring. It allows you to wire in any beans in your Spring context into Jersey and vice-versa.
For instance, if you are using spring-security, it will provide your spring-security principal when wiring the Jersey specific SecurityContext into any of your REST resources.
If you want to access Spring beans from your Jersey REST endpoints (or use Spring Beans as implementations for your JAX-RS interfaces) you need to integrate Spring and Jersey, otherwise it won't work. If you don't have any connections between Spring beans and your REST endpoints, then it is not necessary.
I think your first statement is correct. I have used Jersey and Sprint as separate entities.
Jersey is really awesome to create a web server.
Spring is useful for dependency injection (beans) and other cools stuff.
About your second statement, I do not know anything jersey-spring maven project.
My suggestion/opinion is to do as your first comment. Use them in a separate way. You will have the best of both worlds. Using jersey-spring maven project might be a complication and maybe it is not what you want. Libraries usually are intend to be independent.

Multiple logger library situation in Spring

I have just met situation like that, I created default Spring framework from Spring tool suite, it will use slf4j and log4j for log everything into console.
After that, I add Spring security oauth dependency to maven, this dependency tree like :
spring security oauth --> spring boot --> spring boot starter --> logback(another logger).
The problem is my project had a logger, now spring boot add another logger, this make my logger work so strange (I used log4j.xml in classpath, level of logger is info but it print everything in debug level).
After I exclude logback from spring security oauth dependency from maven, log worked great, but I'm afraid of if I removed logback from spring boot starter, somewhere in this lib need logback, does it throw ClassNotFoundException?
Thanks in advance!
Update:
I copied pom file of spring security oauth2 from it's folder:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>1.0.0.RELEASE</version>
</dependency>
Spring Security OAuth does not rely on Spring Boot. You probably have a different dependency that brings that.
Anyway, you can safely exclude logback, yes. Check also the documentation for more details.

Categories

Resources