HATEOAS methods not found - spring-boot

My controller can't seem to find the HATEOAS methods like "linkTo".
Am I missing something?
pom.xml
<?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>org.springframework</groupId>
<artifactId>provider</artifactId>
<version>0.1.0</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.1.5.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Spring Security -->
<!-- Auto configured, remove dependencies to disable. -->
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
</dependency>
<!-- OAuth 2.0 -->
<dependency>
<groupId>org.springframework.security.oauth</groupId>
<artifactId>spring-security-oauth2</artifactId>
<version>2.0.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test-mvc</artifactId>
<version>1.0.0.M2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<!-- Spring MongoDB -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId>
</dependency>
<!-- Spring REST MVC -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-rest-webmvc</artifactId>
</dependency>
<!-- Dozer: DTO/Entity Mapper -->
<dependency>
<groupId>net.sf.dozer</groupId>
<artifactId>dozer</artifactId>
<version>5.3.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
</dependency>
</dependencies>
<properties>
<start-class>com.provider.core.Application</start-class>
</properties>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-snapshots</id>
<url>http://repo.spring.io/libs-snapshot</url>
<snapshots><enabled>true</enabled></snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-snapshots</id>
<url>http://repo.spring.io/libs-snapshot</url>
<snapshots><enabled>true</enabled></snapshots>
</pluginRepository>
</pluginRepositories>
</project>
Controller
package com.provider.core;
import org.dozer.DozerBeanMapper;
import org.dozer.Mapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.provider.account.Account;
import com.provider.account.AccountDTO;
#Controller
public class AccountController
{
private static final Logger logger = LoggerFactory.getLogger(AccountController.class);
#Autowired private AccountRepository repo;
#RequestMapping(value = "account", method = RequestMethod.POST)
public #ResponseBody HttpEntity<AccountDTO> createAccount(#RequestBody AccountDTO accountDto) {
logger.info("Start createAccount()");
Mapper mapper = new DozerBeanMapper();
Account account = mapper.map(accountDto, Account.class);
Account savedAccount = repo.save(account);
AccountDTO savedAccountDto = mapper.map(savedAccount, AccountDTO.class);
// DOES NOT COMPILE "linkto" not defined.
savedAccountDto.add(linkTo(AccountController.class).slash(savedAccountDto.getId()).withSelfRel());
return new ResponseEntity<AccountDTO>(savedAccountDto, HttpStatus.OK);
}
}

In case you are using HATEOAS v1.0 and above (Spring boot >= 2.2.0), do note that the classnames have changed. Notably the below classes have been renamed:
ResourceSupport changed to RepresentationModel
Resource changed to EntityModel
Resources changed to CollectionModel
PagedResources changed to PagedModel
ResourceAssembler changed to RepresentationModelAssembler
More information available in the official documentation here.
When using Spring boot starter, the below dependency would suffice to include HATEOAS:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-hateoas</artifactId>
</dependency>
Hoping this information will help someone like me who searched for hours to find why Resource class was not getting resolved.

Looks like your POM is missing the spring-hateoas dependency.
So first add this to pom.xml:
<dependency>
<groupId>org.springframework.hateoas</groupId>
<artifactId>spring-hateoas</artifactId>
<version>0.15.0.RELEASE</version>
</dependency>
Then you can add this static import and your code should compile:
import static org.springframework.hateoas.mvc.ControllerLinkBuilder.*

If you are using HATEOAS in eclipse(Version : Oxygen.3a Release (4.7.3a)), please note that the class names have changed.
Resource changed to EntityModel
Resources changed to CollectionModel
More information available in the official documentation below link ->
https://docs.spring.io/spring-hateoas/docs/current/reference/html/
When using Spring boot starter, you've to use below dependency to include HATEOAS:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-hateoas</artifactId>
</dependency>
Demo Code :
EntityModel<Users> resource = new EntityModel<Users>(user);
ControllerLinkBuilder linkTo = linkTo(methodOn(this.getClass()).retrieveAllUsers());
resource.add(linkTo.withRel("all-users"));
Note : You have to import
import static org.springframework.hateoas.server.mvc.ControllerLinkBuilder.*;
Hope this information is helpful to find why Resource class was not getting resolved !!

Add following dependency :-
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-hateoas</artifactId>
<version>2.4.0</version>
</dependency>
Follow Sample Code :-
import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.*;
#GetMapping(path = "/users/{id}")
public EntityModel<User> getUserInfo(#PathVariable int id) {
User user = userDao.getUser(id);
EntityModel<User> entityModel = EntityModel.of(user);
Link link = WebMvcLinkBuilder.linkTo(methodOn(this.getClass()).getUserList()).withRel("user-list");
entityModel.add(link);
if(user == null) {
throw new UserNotFoundException("User not found with id : "+id);
}
return entityModel;
}

Add below dependency in pom.xml,it could resolve your issue.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-hateoas</artifactId>
</dependency>
If above doesn't work then easy solution is got with older versions of hateos.
As new versions of Hateoas has been changed totally, below link can help you.
https://docs.spring.io/spring-hateoas/docs/current/reference/html/
Am mentioning some major changes in methods of Hateoas
ResourceSupport is now RepresentationModel
Resource is now EntityModel
Resources is now CollectionModel
PagedResources is now PagedModel
The LinkDiscoverer API has been moved to the client package.
The LinkBuilder and EntityLinks APIs have been moved to the server package.
ControllerLinkBuilder has been moved into server.mvc and deprecated to be replaced by WebMvcLinkBuilder.
RelProvider has been renamed to LinkRelationProvider and returns LinkRelation instances instead of Strings.
VndError has been moved to the mediatype.vnderror package

There are some nomenclature changes in the recent release(2.2.0 or >). Refer to this document

I'm gonna leave this here in case someone new needed it:
ControllerLinkBuilder is deprecated and you should use WebMvcLinkBuilder instead (check this https://docs.spring.io/spring-hateoas/docs/1.0.0.M1/apidocs/org/springframework/hateoas/server/mvc/ControllerLinkBuilder.html)
so instead if:
import static org.springframework.hateoas.server.mvc.ControllerLinkBuilder.*;
use this:
import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.*;

This issue occurred in my case because there are two hateoas jars one has the resource interface and other has the implementaion with same groupId and arifactId.
To Resolve this issue -->
Downgrade your STS if you're using 4.* to 3.*
Add the below dependency if you're using spring boot
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-hateoas</artifactId>
</dependency>
After adding this dependency run below commands in the command prompt where you're pom.xml is present
mvn clean install package
mvn eclipse:eclipse
This steps resolved issue for me.

Might be helpful for some, I see one thing thats not addressed here and couple of comments ask for this:
Cannot resolve symbol 'hal'
Cannot resolve symbol 'Jackson2HalModule'
hal module is under media now. So
org.springframework.hateoas.hal.Jackson2HalModule
is now replaced with
org.springframework.hateoas.mediatype.hal.Jackson2HalModule

The below imports are required
import org.springframework.hateoas.EntityModel;
import org.springframework.hateoas.Link;
import org.springframework.hateoas.server.mvc.WebMvcLinkBuilder;
With a static import from WebMvcLinkBuilder for all methods

Like others have said, hateoas had a lot of changes. I still was running into the same issues though even with the names properly changed to EntityModel etc. because it wasn't letting me import anything from org.springframework.hateoas.
I was using maven with IntelliJ IDEA and had to right-click my pom.xml, -> maven, -> reload project. Then it let me import things from hateoas properly.
Hope this helps someone!

Related

repackage failed: Unable to find main class when creating a library using Spring-Boot-Starter-Parent version 2.1.2.RELEASE

I created an example resource server in spring security with a library that contains the WebSecurityConfigurerAdapter. When I update the parent spring-boot-starter-parent in the pom for the library from 2.0.8 to 2.1.2 as well as the spring-boot-maven-plugin I get the dreaded repackage failed: Unable to find main class
Version 2.0.8 of the spring-boot-starter-parent doesn't have this issue.
Update: This happens upon doing a mvn clean compile install. A version of the code with parent 2.0.8 can be found here. Just change the pom.xml spring starter parent from 2.0.8 to 2.1.2.
My Library pom is as follows.
<?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.example.utils.security</groupId>
<artifactId>resource-config</artifactId>
<version>1.0.1</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>2.1.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.1.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
<version>5.1.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-oauth2-jose</artifactId>
<version>5.1.3.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.security/spring-security-oauth2-resource-server -->
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-oauth2-resource-server</artifactId>
<version>5.1.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
<version>5.1.3.RELEASE</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.1.2.RELEASE</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
<configuration>
<skip>true</skip>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
A class to define an annotation.
import com.example.utils.security.resource.autoconfig.ResourceServerConfig;
import org.springframework.context.annotation.Import;
import java.lang.annotation.*;
#Target(ElementType.TYPE)
#Retention(RetentionPolicy.RUNTIME)
#Documented
#Import({ResourceServerConfig.class})
public #interface EnableExampleResourceServer {
}
The WebSecurityConfigurerAdapter
package com.example.utils.security.resource.autoconfig;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.PriorityOrdered;
import org.springframework.core.annotation.Order;
import org.springframework.http.HttpMethod;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
#Configuration
#EnableWebSecurity
#Order(PriorityOrdered.HIGHEST_PRECEDENCE + 500)
public class ResourceServerConfig extends WebSecurityConfigurerAdapter {
#Override
protected void configure(HttpSecurity http) throws Exception {
http
.csrf().disable()
.anonymous().disable()
.logout().disable()
.formLogin().disable()
.httpBasic().disable()
.authorizeRequests()
.antMatchers(HttpMethod.OPTIONS).permitAll()
.anyRequest().authenticated()
.and()
.oauth2ResourceServer().jwt();
}
}
I solved this problem by using maven-compiler-plugin instead of spring-boot-maven-plugin. Still to find out what makes spring-boot-maven-plugin to cause this problem though.
Maven plugin details:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
The release notes describe that the repackage goal has an identifier now to allow for easier overriding of the repackage execution.
The build above is a bit odd. If you don't want the repackage to occur, why are you defining the goal at all? Just don't define the goal and repackage will not occur as the parent only offers a default execution when the plugin is explicitly defined.
If you need to define it for a different reason, then the link above explains what you should do. Currently, you are redefining a second execution of the repackage goal.
There could be 2 possibilities -
The location of source directory is wrong. You have to use the sourceSets directive to fix this. your source directory should resemble something like src/main/java/your/package.
OR
No main() method in project (if this is not lib)

Wildfly swarm - Page not found error

This is the pom.xml file that I have
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>hu.javacity.app.buildingsofcities</groupId>
<artifactId>city</artifactId>
<name>WildFly Swarm Example</name>
<version>1.0.0-SNAPSHOT</version>
<packaging>war</packaging>
<properties>
<version.wildfly.swarm>2018.4.1</version.wildfly.swarm>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<failOnMissingWebXml>false</failOnMissingWebXml>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.wildfly.swarm</groupId>
<artifactId>bom-all</artifactId>
<version>${version.wildfly.swarm}</version>
<scope>import</scope>
<type>pom</type>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<finalName>demo</finalName>
<plugins>
<plugin>
<groupId>org.wildfly.swarm</groupId>
<artifactId>wildfly-swarm-plugin</artifactId>
<version>${version.wildfly.swarm}</version>
<executions>
<execution>
<goals>
<goal>package</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<!-- Java EE 7 dependency -->
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
<scope>provided</scope>
</dependency>
<!-- WildFly Swarm Fractions -->
</dependencies>
</project>
And this would be HelloWorldEndpoint.java
package hu.javacity.app.buildingsofcities.city.rest;
import javax.ws.rs.Path;
import javax.ws.rs.core.Response;
import javax.ws.rs.GET;
import javax.ws.rs.Produces;
#Path("/hello")
public class HelloWorldEndpoint {
#GET
#Produces("text/plain")
public Response doGet() {
return Response.ok("Hello from WildFly Swarm!").build();
}
}
After running Maven, It says: "WildFly Swarm is ready" and I get no errors So im trying to write the following URL in: localhost:8080/hello The result is the following message: "not found" I tried using Edge, Firefox or Chrome, but to no success.
So there are a few problems here. First, you are missing the JAX-RS Swarm Fraction. This means that the dependencies in your pom.xml need to look like:
<dependencies>
<!-- Java EE 7 dependency -->
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
<scope>provided</scope>
</dependency>
<!-- WildFly Swarm Fractions -->
<dependency>
<groupId>org.wildfly.swarm</groupId>
<artifactId>jaxrs</artifactId>
</dependency>
</dependencies>
Now the second problem. In the 2018.4.1 version of Wildfly Swarm the swarm team removed the automatic generation of the javax.ws.rs.core.Application that is required for standard JAX-RS applications (see this blog for more information). If you try to start your code in 2018.4.1 you'll get a warning:
WFLYRS0015: No Servlet declaration found for JAX-RS application. In
demo.war either provide a class that extends
javax.ws.rs.core.Application or declare a servlet class in web.xml.
And your application doesn't work. So you have two choices to fix that issue. The first is to just use 2018.3.1 as it does do the auto generation. However, that's a poor fix as, going forward, Swarm will likely continue the behavior of the 2018.4.1 release. So to fix it you need to add your own javax.ws.rs.core.Application that could look something like:
package hu.javacity.app.buildingsofcities.city.rest;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
#ApplicationPath("/")
public class RestApplication extends Application {
// intentionally empty
}
This sets up your JAX-RS path to be /. A common pattern is to use something like /rest or /svc but for now this will work fine for you.

Why cant i import #WithMockUser to my test

I am trying to do an authentication test with #With Mock User but it is refusing to import to my test class in spring boot . This is the class configuration
#WebAppConfiguration
#AutoConfigureMockMvc
#RunWith(SpringJUnit4ClassRunner.class)
#SpringBootTest(classes = AdminPortalApplication.class)
public class BookControllerTest {
#WithMockUser not able to import ,its red in color showing that spring boot does not recognize it, I used this dependency and property
<spring-security.version>4.0.2.RELEASE</spring-security.version>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
Keeps saying cannot resolve symbol #WithMockUser
Try fresh dependency. I just solved such issue with this one:
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
Add an import statement for the library:
import org.springframework.security.test.context.support.WithMockUser;
And declare a dependency in your pom:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
Try forcing gradle to refresh your dependencies:
gradle --refresh-dependencies

spring boot + jboss = page 403

I was trying to connect spring boot with jboss for a week... I read many topics on this forum and many of them are too old. I get error 404 every time when i want run server. Can you please help me to config my app with jboss properly?
UPDATE:
Thank you for help with error 404. I changed package to "war". But now when I run wildfly server Im getting error 403 (forbidden). I was looking how to fix it and still stay in the same place. It is so hard to get wildfly work with spring boot...
I updated files:
https://github.com/kuzyn007/LibraryCRUD
pom.xml
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>pl.seweryn</groupId>
<artifactId>LibraryCRUD</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<!-- Spring: boot starter parent -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.0.RELEASE</version>
</parent>
<dependencies>
<!-- Starter for building web, including RESTful, applications using Spring
MVC. Uses Tomcat as the default embedded container -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Starter for using Spring Data JPA with Hibernate -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
<!-- Starter for testing Spring Boot applications with libraries including
JUnit, Hamcrest and Mockito -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- The spring-boot-devtools module can be included in any project to
provide additional development-time features. -->
<!-- Applications that use spring-boot-devtools will automatically restart
whenever files on the classpath change. -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<!-- JSP Standard Tag Library -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<dependency>
<groupId>org.jboss.spec.javax.servlet.jsp</groupId>
<artifactId>jboss-jsp-api_2.2_spec</artifactId>
<version>1.0.2.Final</version>
</dependency>
<!-- … -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<!-- … -->
</dependencies>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>
<build>
<finalName>LibraryCRUD</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork>
</configuration>
</plugin>
</plugins>
</build>
</project>
application.properties
# MVC
spring.view.prefix=/WEB-INF/jsp/
spring.view.suffix=.jsp
# JNDI
spring.datasource.jndi-name=java:jboss/datasources/library
# JPA/HIBERNATE
spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.hibernate.naming.physical-strategy=pl.seweryn
spring.jpa.database=H2
spring.jpa.show-sql=true
Application.java
package pl.seweryn.init;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;
//#Configuration
//#EnableAutoConfiguration
//#ComponentScan
#SpringBootApplication // same as #Configuration #EnableAutoConfiguration #ComponentScan - alternative
public class Application extends SpringBootServletInitializer {
#Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Application.class);
}
public static void main(String[] args) throws Exception {
SpringApplication.run(Application.class, args);
}
}
Follow this instructions: http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#howto-create-a-deployable-war-file
You basically have to package your app as a WAR and implement SpringBootServletInitializer.
I found many mistakes in my project and now its working. I will write here answer for my app.
Physical naming was wrong. It should be: spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl. Or I could delete this line and it will be provided default from spring boot
I have Application.class in pl.seweryn.init package. I added #ComponentScan(pl.seweryn)
BookDaoImpl was not a spring component. Added line #Component

Spring Boot GS: ComponentScan and ClassNotFoundException for ConnectionFactory

I'm playing sur the Spring Boot getting started guide but the auto configuration fails and I get:
java.lang.ClassNotFoundException: javax.jms.ConnectionFactory
It seems it's due to the location of the Application class. Where should it be located? At the top-level package (src/main/java) or in a specific package?
Your Application class should be placed in a specific package and not in the default (top-level) package. For example, put it in com.example and place all your application code in this package or in sub-packages like com.example.foo and com.example.bar.
Placing your Application class in the default package, i.e. directly in src/main/java isn't a good idea and it will almost certainly cause your application to fail to start. If you do so, you should see this warning:
** WARNING ** : Your ApplicationContext is unlikely to start due to a #ComponentScan of the default package.
Do not put the bootup Application Class in default package. This will solve the problem.
Working code:
package com.spring.boot.app;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
You need the main class to be inside a package. Because Spring boot annotation #SpringBootApplication will look for a package to scan while launching the app.
So make sure there is a package statement on top of your main class file. That's it.
I had got the same Problem,
Soon I realized that I hadn't included my MAIN method in package.
After including main inside the package , spring boot performed without glitches.
Sample program (Basic) -
package springbootquickstart;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
public class application {
public static void main(String[] args) {
// TODO Auto-generated method stub
SpringApplication.run(application.class, args);
}
}
Your configuration should looks like this and the Application.java should be at the root of you packages E.g /src/main/java/io/eddumelendez
io.eddumelendez is my package
<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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.2.1.RELEASE</version>
<relativePath />
</parent>
<groupId>io.eddumelendez.jms</groupId>
<artifactId>spring-boot-jms-sample</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>qa</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-broker</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

Resources