I have following section in application.yaml file where I am trying to read an external property file:
spring:
config:
name: my-config
location: ./config/
pom.xml looks like:
<?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>myservice</groupId>
<artifactId>my-service</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.jayway.jsonpath</groupId>
<artifactId>json-path</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>org.everit.json</groupId>
<artifactId>org.everit.json.schema</artifactId>
<version>1.3.0</version>
</dependency>
</dependencies>
<properties>
<java.version>1.10</java.version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Application.java:
#SpringBootApplication
#EnableAutoConfiguration
#ComponentScan
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
Config Class where I am trying to use the config:
#Component
#ConfigurationProperties(prefix = "my-config-prefix")
#RefreshScope
public class MyConfig {
...
When I try to run the Microservice it does not map any of the config elements from the external config my-config.yml file.
If I copy and paste the entire content of the my-config.yml into application.yml file, it works as expected mapping all the elements in MyConfig class!
Spring boot seem to pick up all the elements under application.yml file for instance if I change the server.port property I can see the change when I restart however it looks like it is not picking up any properties under 'spring' element.
I have tried to supply the property on commands line and it works as expected however I want this properties in the yml file:
--spring.config.location=file:///Users/home/my-config.yml
Also, I have tried to specify the full location (as above) from application.yml file and it does not work.
I have manage to resolve the issue by renaming the application.yml file to bootstrap.yml.
It looks like only bootstrap properties can access 'spring.*' values.
Related
I trying to add log4j2 to spring boot project but it is giving below error.
ERROR StatusLogger Log4j2 could not find a logging implementation. Please add log4j-core to the classpath. Using SimpleLogger to log to the console
I have configured same using this link
https://howtodoinjava.com/spring-boot2/spring-boot-log4j2-config/
package log.demo.LogDemo;
/**
* Hello world!
*
*/
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.ApplicationContext;
#SpringBootApplication
public class Application extends SpringBootServletInitializer {
private static final Logger LOGGER = LogManager.getRootLogger();//Application.class);
public static void main(String[] args)
{
ApplicationContext ctx = SpringApplication.run(Application.class, args);
LOGGER.info("Info level log message");
LOGGER.debug("Debug level log message");
LOGGER.error("Error level log message");
}
}
<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>log.demo</groupId>
<artifactId>LogDemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>LogDemo</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/resources</directory>
<excludes>
<!-- <exclude>application.properties</exclude> <exclude>TahitiConfig.xml</exclude>
<exclude>job_definitions.xml</exclude> -->
</excludes>
</resource>
</resources>
</build>
</project>
Don't understand what went wrong any help will be great.
Please let me know if required more information
Tried this solution but no help
Log4j2 could not find a logging implementation with Sprint Boot
Add dependency to your pom
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-to-slf4j</artifactId>
<version>2.8.2</version>
</dependency>
This is because for my case the log4j is printing from Elasticsearch. I have implemented logback.xml for logging.
To overcome default log4j error messages, you can override with slf4j.
I have this problem in NetBeans 8.0 when I added the jar log4j-api-2.12.2.jar.
Netbeans print this message in the console.
"StatusLogger Log4j2 could not find a logging implementation. Please add log4j-core to the classpath. Using SimpleLogger to log to the console..."
I resolved this problem when I added log4j-core-2.12.2.0001L.jar in my project.
I supposed in Spring, just most add the dependency log4j-core in maven.
Link download JAR
https://jar-download.com/?search_box=log4j-core
I can run my Camel application using the camel-maven-plugin within maven (mvn camel:run). The camel-context.xml file is read and my routes start correctly.
My issues come about when I try to execute these camel routes on spring. When spring starts, I do not see any logs from Camel like I did when running the camel plugin directly. I also dont have any evidence that anything camel related has started. What is my configuration missing to successfully run the application? I am currently attempting to run this via an embedded tomcat instance (see mvn profile below). I would imagine that there is something unique that I need to do in order to get spring to find the camel context.
Thanks for any and all help!
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>
<parent>
<groupId>---</groupId>
<artifactId>---</artifactId>
<version>1.0.6-SNAPSHOT</version>
</parent>
<artifactId>---</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>${packaging.type}</packaging>
<properties>
<jacoco.minimum.code.coverage>0.8</jacoco.minimum.code.coverage>
<packaging.type>war</packaging.type>
<failOnMissingWebXml>false</failOnMissingWebXml>
<org.apache.camel.version>2.16.0</org.apache.camel.version>
</properties>
<dependencies>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-csv</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-core</artifactId>
<version>2.19.2</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-aws</artifactId>
<version>2.19.2</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-spring</artifactId>
<version>2.19.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.camel</groupId>
<artifactId>camel-maven-plugin</artifactId>
<version>2.19.2</version>
</plugin>
</plugins>
</build>
<profiles>
<!-- Default build profile for generating war -->
<profile>
<id>war</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<packaging.type>war</packaging.type>
<log.dir>${catalina.base}/logs</log.dir>
<!-- updates bootstrap.properties -->
<config.override.path>file:${catalina.base}/conf</config.override.path>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.6</version>
<configuration>
<descriptor>/src/main/resources/deployablecontent.xml</descriptor>
<tarLongFileMode>posix</tarLongFileMode>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
<!-- Build profile for stand-alone java application with embedded Tomcat
Container -->
<profile>
<id>embedded</id>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
<properties>
<packaging.type>jar</packaging.type>
<log.dir>logs</log.dir>
<!-- updates bootstrap.properties -->
<config.override.path>./conf</config.override.path>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.camel</groupId>
<artifactId>camel-maven-plugin</artifactId>
<version>2.19.2</version>
</plugin>
</plugins>
</build>
</profile>
</profiles>
My Routebuilder class:
public class PriorityCodeSourcesUpdaterRouteBuilder extends RouteBuilder {
private Endpoint incomingEndpoint;
private Endpoint outgoingEndpoint;
#Override
public void configure() throws Exception {
from(incomingEndpoint)
.process((exchange) -> {
System.out.println("new file received");
})
.to(outgoingEndpoint);
}
/**
* Set the incoming endpoint from the spring config file.
* #param incomingEndpoint incoming endpoint
*/
public void setIncomingEndpoint(final Endpoint incomingEndpoint) {
this.incomingEndpoint = incomingEndpoint;
}
/**
* Set the outgoing endpoint from the spring config file.
* #param outgoingEndpoint outgoing endpoint
*/
public void setOutgoingEndpoint(final Endpoint outgoingEndpoint) {
this.outgoingEndpoint = outgoingEndpoint;
}
}
My camel-context.xml that lives in resources/META-INF/spring/:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:camel="http://camel.apache.org/schema/spring" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
">
<camel:camelContext id="camel">
<camel:routeBuilder ref="PriorityCodeSourcesUpdaterRouteBuilder"/>
<camel:endpoint id="incomingEndpoint" uri="">
<camel:property key="accessKey" value=""/>
<camel:property key="secretKey" value="RAW()"/>
<camel:property key="region" value=""/>
<camel:property key="deleteAfterRead" value="false"/>
</camel:endpoint>
<camel:endpoint id="outgoingEndpoint" uri="file://#{systemProperties['java.io.tmpdir']}">
<camel:property key="fileName" value="deadBeefName"/>
<camel:property key="readLock" value="markerFile "/>
<!-- We need a customer idempotentKey because all files sent to this endpoint have the same fileName. -->
<!-- This will prevent camel from thinking that it has already consumed the file. -->
<!--<camel:property key="idempotentKey" value="3"/>-->
</camel:endpoint>
</camel:camelContext>
<bean id="PriorityCodeSourcesUpdaterRouteBuilder" class=".....PriorityCodeSourcesUpdaterRouteBuilder">
<property name="incomingEndpoint" ref="incomingEndpoint" />
<property name="outgoingEndpoint" ref="outgoingEndpoint" />
</bean>
TL;DR:
Try adding the camel-spring-boot-starter dependency to your POM file, mark your route with #Component annotation and add a #SpringBootApplication class to start your Spring Context tied up with Camel.
Reading through your files I'm guessing that you are using Spring Boot, right?
If that so, it's nice to have the following dependencies in your POM:
<dependencyManagement>
<dependencies>
<!-- Spring Boot BOM -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring.boot-version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- Camel BOM -->
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-spring-boot-dependencies</artifactId>
<version>${camel.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
This are the BOM (Bills of Materials) from both Spring Boot and Camel. This way all your required dependencies will be resolved nicely, plus avoiding the need to describe camel component's versions all the time.
It's a requirement to have the camel-context.xml file? If it isn't you, could define everything on your RouteBuilder class and put a #SpringBootApplication annotation class on your classpath.
From the docs:
Spring Boot component provides auto-configuration for Apache Camel. Our opinionated auto-configuration of the Camel context auto-detects Camel routes available in the Spring context and registers the key Camel utilities (like producer template, consumer template and the type converter) as beans.
camel-spring-boot jar comes with the spring.factories file, so as soon as you add that dependency into your classpath, Spring Boot will automatically auto-configure Camel for you.
The problem I think you're facing is that there's no "glue" between your Spring Context and the Camel Context (camel-context.xml file). Add the #Component annotation to your RouteBuilder and the following dependecy:
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-spring-boot-starter</artifactId>
</dependency>
Then these routes will be started automatically. To keep the main thread blocked so that Camel stays up, either include the spring-boot-starter-web dependency, or add camel.springboot.main-run-controller=true to your application.properties or application.yml file.
There's more information and examples in the docs.
Cheers!
As requested, below is a simplified version of Camel-Spring setup that we've been using in our Camel projects. Spring Boot is used (the goodies it offers are just too good to ignore, IMO) and we're also using web starter. Since you're using WAR packaging anyway, this shouldn't be an issue to you.
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">
<artifactId>stackoverflow</artifactId>
<groupId>sandbox</groupId>
<version>1.0-SNAPSHOT</version>
<modelVersion>4.0.0</modelVersion>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>1.5.3.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-spring-boot-starter</artifactId>
<version>2.19.2</version>
</dependency>
</dependencies>
</project>
src/main/java/stackoverflow/CamelRoute.java (the route definition, discovered automatically on startup, by the virtue of being placed in the package path of the #SpringBootApplication class - the TestApp class below):
package stackoverflow;
import org.apache.camel.builder.RouteBuilder;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
#Component
public class CamelRoute extends RouteBuilder {
#Value("${message}")
private String message;
// you can also have dependencies #Autowired here
#Override
public void configure() {
from("direct:test").process(exchange -> {
exchange.getIn().setBody(message);
});
}
}
src/main/resources/application.properties (to illustrate how can configuration values be passed on to your route definitions):
message=Test
src/main/resources/log4j.properties (mostly so that you would see your route started in the logs):
log4j.rootLogger=INFO, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%-5p %c:%L - %m%n
src/main/java/stackoverflow/TestApp.java:
package stackoverflow;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
public class TestApp {
public static void main(String[] args) {
SpringApplication.run(TestApp.class, args);
}
}
The TestApp class starts the application, and keeps running until stopped (it starts an embedded Tomcat server). The routes are discovered and started on app startup.
This setup prefers Java Configuration over XML, but if you still prefer the XML, you can import your configuration using #ImportResource annotation on your TestApp. You will also be able to autowire the XML-configured classes into your route definitions.
Let me know if you have any questions about this setup.
I am learning Spring Boot. I have just created my first project using maven, Spring Boot, Spring Rest support and MongoDB. It compiles successfully, but it resolves all the dependencies, but do not compile the java classes at all.
After compilation, jar file is correctly created, it contain lib folder, metadata etc, but it do not contain project class file at all.
Hence when i run the project with mvn spring-boot:run, it throws an exception that class not found (Main method class for Spring boot initialization).
Please suggest, what I am doing wrong here, here is my maven configuration class:
<?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>
<properties>
<java.version>1.6</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<start-class>com.assignment.BootInitializer</start-class>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.1.9.RELEASE</version>
</parent>
<groupId>com.assignment</groupId>
<artifactId>spring-assignment</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
And here is the main initializer class:
package com.assignment;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
#Configuration
#EnableAutoConfiguration
#ComponentScan
public class BootInitializer {
public static void main(String[] args) {
SpringApplication.run(BootInitializer.class, args);
}
}
What I need to do, to ensure that maven is compiling the java classes and including them in the jar file.
Thanks.
I am learning Spring Boot.
Okay, good. Let's do one step at a time. Go to start.spring.io and generate a template project with whatever dependencies you want and whatever build tool (maven / gradle) you like. Build it and run it and see whether it is coming up or not. Then incrementally build on top of it.
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
I am trying to implement a REST based MongoDB service through Spring, but I have run into some trouble. I cannot import a certain library.
I have this in my class:
package main;
import java.util.List;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;
#RepositoryRestResource(collectionResourceRel = "people", path = "people")
public interface PersonRepository extends MongoRepository<Person, String> {
List<Person> findByLastName(#Param("name") String name);
}
But the import org.springframework.data.rest.core.annotation.RepositoryRestResource;
is not working for some reason and is giving me the error: The import org.springframework.data.rest cannot be resolved
This is my maven 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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>UserRegistrationServices</groupId>
<artifactId>UserRegistrationServices</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<!-- Inherit defaults from Spring Boot -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.2.5.RELEASE</version>
</parent>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!-- Get the dependencies of a web application -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-rest</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<!-- Spring Boot Maven Support -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Also, I am directly following this guide: http://spring.io/guides/gs/accessing-mongodb-data-rest/
How can this be resolved? Thanks
Edit your pom.xml.... and add these lines inside <dependencies></dependencies>...
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-rest</artifactId>
<version>1.2.5.RELEASE</version>
</dependency>
I had a same problem on 1.3.6.RELEASE.
It said,
org.springframework.data.rest.core.annotation.RepositoryRestResource cannot be resolved to a type
So I set the version of spring-boot as 1.2.8.RELEAS, now I solve the problem.
My current spring-boot's pom.xml is below.
....
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.2.8.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
....
I had the same problem with this error message. In my case was the cause a refresh problem in the IDE (Intellij).
Deleting the line with the annotation (#RepositoryRestResource) and reinsert it helped a lot.
I also had tried to follow the same from https://spring.io/guides/gs/accessing-mongodb-data-rest/
For me, this issue got resolved after I added the version from https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-rest/2.3.0.RELEASE
to be more precise and clear, I added this in the pom.xml file to solve this issue. Maybe if you add the version it will start working!
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-rest -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-rest</artifactId>
<version>2.3.0.RELEASE</version>
</dependency>