Spring Boot with jersey and usage of Hateaos - spring-boot

I have been going through many tutorials and searching web sometime now. But i haven't been able to get a correct website where i get an example of HATEOAS being implemented in a Spring Boot with Jersey as starter project.
can any one please guide me here.
We have below in pom.xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.7.RELEASE</version>
<relativePath />
</parent>
<dependencies>
<dependency>
<!-- only dependency needed -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jersey</artifactId>
<!-- <exclusions> <exclusion> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId> </exclusion> </exclusions> -->
</dependency>
</dependencies>
So in my application controller i would be using #Path to configure api end points. I am unable to use Hateoas library as is as they explain and be best suitable with Spring Rest and not Jersey.
So could someone please point me in right direction, I want to embed hypermedia into my rest response. Which is the best standard? HAL or any other? Which library supports them ? please help

Related

integrate spring security and spring MVC and and spring integration with each other

I am currently developing a web site for having control over IOT devices by spring.
The application is based on spring boot.
I have developed most of rest API s by extending from DataJpa.
Next step I had to implement TCP connection in my application and after doing some
research I adopt spring integration for doing that and simple application worked well.
Next, I decided to add spring Integration to my application for only user authentication after that I configured that I realized that there are some conflicts
in my application and I received java: cannot access javax.servlet.Filter class file for javax.servlet.Filter not found.
I did some research in I found a related topic for spring webflux and it was said that I should implement spring security for webflux (integration in this case) not for spring web.
Now about the main problem: as I said earlier the structure of application should be like this:
1- first part which is MVC based and there are some webpages and data should be stored in mySql or mariaDB, also i want to authenticate and authorize users by spring security.
2-Second part of application is implementation Tcp socket via Netty or spring integration for having an an alive connection between IOT devices and Server.
Now I am looking for a way to be able to say to spring that it should distinguish
these two different contexts from each other and not combine configurations with the other one while they must work with each other.
Is there any way for this separation and tell spring combine them with special responsible for each?
note that adding following dependency did not change anyThing
`<dependency>
<groupId>jakarta.servlet</groupId>
<artifactId>jakarta.servlet-api</artifactId>
<version>6.0.0</version>
<scope>provided</scope>
</dependency>`
other dependencies :
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-ip</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.mariadb.jdbc</groupId>
<artifactId>mariadb-java-client</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-core</artifactId>
</dependency>
security Config :
#Configuration
#EnableWebSecurity
public class securityConfig extends WebSecurityConfigurerAdapter {
#Autowired
appUserDetailService appUserDetailService;
#Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(appUserDetailService);
}
#Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/admin").hasRole("ADMIN")
.antMatchers("/user").hasAnyRole("ADMIN", "USER")
.antMatchers("/","/home").permitAll()
.and().formLogin();
}
#Bean
public BCryptPasswordEncoder bCryptPasswordEncoder() {
return new BCryptPasswordEncoder();
}
}
error which occurs :
java: cannot access javax.servlet.Filterclass file for javax.servlet.Filter not found
and points to first line of spring security configuration.
my application properties:
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp
spring.datasource.url=jdbc:mariadb://localhost:3307/testapp
spring.datasource.username=root
spring.datasource.password=
spring.datasource.driver-class-name=org.mariadb.jdbc.Driver
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true
server.port=8081
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MariaDBDialect
/C:/Users/s.movahedi/Downloads/t1/demo/src/main/java/com/example/demo/security/securityConfig.java:[17,8] cannot access javax.servlet.Filter [ERROR] class file for javax.servlet.Filter not found
So it's your demo app that needs to be changed to use jakarta instead of javax if you want to use Boot 3, Spring 6.

Using Spring-Actuator in combination with ObjectDB

I am implementing a program in Spring-Boot using ObjectDB. To actually use ObjectDB I have followed this approach https://www.objectdb.com/forum/2523 which is working perfectly.
However, as soon as I want to use Spring-Actuator then I am getting the following errors:
Error creating bean with name 'org.springframework.boot.actuate.autoconfigure.metrics.orm.jpa.HibernateMetricsAutoConfiguration': Injection of autowired dependencies failed; nested exception is com.objectdb.o.UserException: Unsupported unwrap(org.hibernate.SessionFactory.class) for EntityManagerFactory
Any ideas on how to solve this error?
I am using the exact same code as in the link. I have added Spring-Actuator in the pom like this:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
The failure is occurring because Spring Boot is trying to unwrap the JPA EntityManagerFactory to get Hibernate's SessionFactory. It's attempting to do this as you have Hibernate on the classpath (it's a dependency of spring-boot-starter-data-jpa). According to the JPA specification, unwrap should throw a PersistenceException "if the provider does not support the call". Spring Boot catches PersistenceException in case unwrap isn't supported. Unfortunately, ObjectDB is throwing com.objectdb.o.UserException which isn't spec compliant. I'd recommend raising this as an ObjectDB bug.
You can avoid the problem by excluding Hibernate from the classpath. This will stop Spring Boot from attempting to unwrap Hibernate's SessionFactory, preventing the ObjectDB bug from occurring. You can do so by adding an exclusion to the spring-boot-starter-data-jpa dependency in your pom.xml:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<exclusions>
<exclusion>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
</exclusion>
</exclusions>
</dependency>

control is not coming to view i. JSP /HTML in Spring boot

I am new in Spring boot. I have created a Spring boot project(Spring MVC + JDBC) from Spring initializer https://start.spring.io/. My MVC controller is working fine i.e the control is coming to the controller. From the controller, the control is not going to view (JSP/HTML). I have mentioned the JSP path also in application.properties as follows. The control is not going to JSP. I also tried with HTML but the control is not going to any HTML also. Please let me know if anyone can help me out with this.
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp
I have also included below dependencies.
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<scope>provided</scope>
</dependency>

spring core version updation in spring boot parent starter 1.5.10.RELEASE

Here is the maven dependency I have
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.10.RELEASE</version>
<scope>import</scope>
<type>pom</type>
</dependency>
</dependencies>
what I'm trying to achieve is, change the spring core from 4.3.14.RELEASE to 4.3.4.RELEASE. Yes, by default spring boot starter, is using spring core 4.3.14 which can be extended to 5.0.7.RELEASE for that I tried excluding spring-core and including with 4.3.4.RELEASE
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.10.RELEASE</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
</exclusion>
</exclusions>
<scope>import</scope>
<type>pom</type>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.4.RELEASE</version>
</dependency>
</dependencies>
When i tried to build it throws the following expection
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.security.config.annotation.web.configuration.WebSecurityConfiguration': Unsatisfied dependency expressed through method 'setFilterChainProxySecurityConfigurer' parameter 1; nested exception is org.springframework.beans.factory.BeanExpressionException: Expression parsing failed; nested exception is java.lang.NoSuchMethodError: org.springframework.core.convert.support.DefaultConversionService.getSharedInstance()Lorg/springframework/core/convert/ConversionService;
Manually adding each and every dependency is not possible because the application is using a hell lot of dependencies and compatibility with other dependencies would be another tough job.
So is there any workaround for this?
NOTE:- tried to work with spring framework-bom and spring IO and yet didn't find any solution maybe I missing something. Examples of those are welcomed.
You shouldn't really do this. Spring Framework > Spring Boot > Spring Cloud > Data Flow all manage these dependencies for a reason. You should let spring handle the dependnecies and upgrade your implementations as needed.
https://spring.io/blog/2016/04/13/overriding-dependency-versions-with-spring-boot
Answers this in more detail.
It is a reasonable thing to want to do this, but it should be done
with caution, because newer versions of transitive dependencies can
easily break features that rely on the older version in Spring
Boot. When you do this, and apply one of the fixes below, you are
divorcing yourself from the dependency management of Spring Boot and
saying “hey, I know what I am doing, trust me.” Unfortunately,
sometimes you need to do this in order to take advantage of new
features in third party libraries. If you don’t need the new version
of Reactor (or whatever other external transitive dependency you
need), then don’t do this, just stick to the happy path and let Spring
Boot manage the dependencies.

Conflict between Spring data MongoDb and Elasticsearch

I started a project in which I use both Mongo, Elasticsearch and spring boot.
With either technologies by itself, the project works just fine. However with both together, they conflict. I saw this particular article that seemed to be similar to my issue.
https://jira.spring.io/browse/DATAES-57
So I tried it out and it the problem is still there.
I put these on the Main class
#EnableAutoConfiguration(exclude = MongoRepositoriesAutoConfiguration.class)
#EnableMongoRepositories(basePackages = "com.searchizi.mongo.repository")
#EnableElasticsearchRepositories(basePackages = "com.searchizi.elasticsearch.repository")
#ComponentScan
public class Application implements CommandLineRunner { … }
A shortened form the the exception trace is this
The class SearchiziUser is in the com.searchizi.mongo.model package. It is not on the Elasticsearch scan path.
Caused by: java.lang.IllegalArgumentException: Unable to identify index name. SearchiziUser is not a Document. Make sure the document class is annotated with #Document(indexName="foo")
at org.springframework.util.Assert.isTrue(Assert.java:65)
at org.springframework.data.elasticsearch.core.ElasticsearchTemplate.getPersistentEntityFor(ElasticsearchTemplate.java:869)
at org.springframework.data.elasticsearch.core.ElasticsearchTemplate.createIndexIfNotCreated(ElasticsearchTemplate.java:684)
at org.springframework.data.elasticsearch.core.ElasticsearchTemplate.createIndex(ElasticsearchTemplate.java:135)
at org.springframework.data.elasticsearch.repository.support.AbstractElasticsearchRepository.createIndex(AbstractElasticsearchRepository.java:80)
at org.springframework.data.elasticsearch.repository.support.AbstractElasticsearchRepository.<init>(AbstractElasticsearchRepository.java:72)
at org.springframework.data.elasticsearch.repository.support.SimpleElasticsearchRepository.<init>(SimpleElasticsearchRepository.java:36)
The scanning for each repository type should be separated but apparently it is not. Any idea what to do?
This is clearly a bug in Spring Data Elasticsearch as it seems to scan for domain types in packages it actually shouldn't. I filed DATAES-?? for you. Also, I filed a ticket so that Spring Data Elasticsearch supports the new multi-store configuration improvements so that you shouldn't have to explicitly configure separate packages.
On a side note, excluding the auto-configuration is not necessary if you set up #EnableMongoRepositories as it will automatically disable Spring Boot's auto-configuration.
I faced this exception and I resolved by change version of elasticsearch and mongodb lib versions
<!-- Spring data mongodb -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId>
<version>1.7.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-commons</artifactId>
<version>1.10.0.RELEASE</version>
</dependency>
<!-- mongodb java driver -->
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
</dependency>
<!-- ELASTICSEARCH -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-elasticsearch</artifactId>
<version>1.2.0.RELEASE</version>
</dependency>

Resources