Using #ControllerAdvice as a dependency for another project - spring

I make a common project for listening Exceptions and use #ControllerAdvice with #ExceptionHandler
#ControllerAdvice(annotations = RestController.class)
public class RestResponseEntityExceptionHandler extends ResponseEntityExceptionHandler {
#ExceptionHandler({Exception.class})
#ResponseStatus(value = HttpStatus.NOT_FOUND)
#ResponseBody
public ResponseEntity<ErrorResponse> notFound(Exception ex) {
return new ResponseEntity<>(
new ErrorResponse(ex.getMessage(), 404, "My Custom Message"), HttpStatus.NOT_FOUND);
}
}
without any #SpringBootApplication and main method to adding as dependency to my another project. my 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>com.test</groupId>
<artifactId>spring-commons-rest</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>spring-commons-rest</name>
<description>for Listening Exceptions</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<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>
</dependencies>
</project>
after adding its dependency and #ComponentScan to another project, but its not working as Exception handling for response custom Error. I appreciated any help.

I found that was not about using #ComponentScan and with using #Import it will work so fine. but if anyone has any better solution or some guiding line I will appreciate that.

I've solved it by adding
#Order(Ordered.HIGHEST_PRECEDENCE)
to the ControllerAdvice class that you are importing.
In my case, there was another ControllerAdvice class in the downstream project that was taking priority over the one I was importing.

Related

spring #controller vs #RestController error whitelabel error

I created a basic Spring MVC project in STS IDE.
When I try to add a controller I add the annotation #Controller and run the spring boot program, I get the following error:-(Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.), but when I add a #RestController the method returns the 'hello' string. Please help me out on how to fix it using #Controller and please let me know why this happened.
Sample code:
package com.adithya.spring.controllers;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
#Controller
public class ApplicationController {
#RequestMapping("/")
public String Hello() {
return "Hello";
}
}
Also, my pom.xml file is as below:
<?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.adithya</groupId>
<artifactId>spring</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>MyApplication</name>
<description>Login & Reg</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<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>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
Project directory structure .

Spring boot getting 401 unauthorized status code for simple get request

I am very new to Spring framework. I have created a new Spring Starter Project with following modules: web, mongo, security.
I have created a simple controller
#RestController
#RequestMapping("/users")
public class UserController {
private UserRepository userRepository;
#GetMapping("/all")
public List<User> getAllUsers(){
List<User> users = this.userRepository.findAll();
return users;
}
#PostMapping("/")
public void insert(#RequestBody User user){
this.userRepository.save(user);
}
}
And seeded some raw data to the database. When I make request to this route in Postman, I get the following response:
{
"timestamp": 1511113712858,
"status": 401,
"error": "Unauthorized",
"message": "Full authentication is required to access this resource",
"path": "/users/all"
}
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>ngt</groupId>
<artifactId>someArtifact</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>dermaskin</name>
<description>Demo project for Spring Boot with mongodb</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.8.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<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>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
What is causing the unauthorized response and how to disable it for the /all route? Thanks!
#Configuration
public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {
#Override
public void configure(HttpSecurity http) throws Exception {
http.requestMatchers().antMatchers("/users/all").permitAll();
}
}
You need to configure Spring Security, by default all routes all secured for authrorization.
The code above disables security only for "/users/all" URL.
I was getting this error as I included
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-oauth2-client</artifactId>
</dependency>
in my pom.xml file.
Just remove it and try again.
You can keep your security dependency but then you have to setup a userid and a password. This can be done by adding the following into your application.properties file located under
src/main/resources
folder
security.user.name=user # Default user name.
security.user.password= # your password here

Spring-boot scheduler runs without #EnableScheduling annotation

I was following this example to create a scheduled task in a sample project: https://spring.io/guides/gs/scheduling-tasks
It says, #EnableScheduling ensures that a background task executor is created. Without it, nothing gets scheduled.
But, by mistake i didn't use it. How come it still works?
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>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
Other Classes:
#SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
#Component
public class ScheduledTaskRunner {
Logger log = LoggerFactory.getLogger(ScheduledTaskRunner.class);
#Scheduled(cron = "0/1 * * * * *")
public void run(){
log.info("Hello");
}
}
Isn't the scheduled task supposed to not run?
Your code added the spring-boot-starter-actuator when that is added a scheduler is added as well. This scheduler is used by the actuator to schedule the automatic export of the metrics.
The code for this is in the MetricExportAutoConfiguration and has been added in Spring Boot 1.3.0.
So if you remove the actuator scheduling won't work anymore.

SpringBootApplication cannot be resolved to a type

Hi below is my Spring code and pom.xml. Maven is not able to update even though I tried to force project update. Any resolution on this or direction where I am getting wrong.
package com.boot;
import org.springframework.*;
#SpringBootApplication
public class App
{
public static void main( String[] args )
{
System.out.println("Hello World");
}
}
pom
<groupId>com.boot</groupId>
<artifactId>das-boot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.2.RELEASE</version>
</parent>
<name>das-boot</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<start-class>com.boot.App</start-class>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
I am not getting why Maven is not able to get the dependencies for my project. A
I faced the same issue.
I changed the version from 2.0.5.RELEASE to 2.0.3.RELEASE to make it work
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
If you are fine to move to 2.x then please follow below steps:
Step 1 copy below pom
<?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.boot</groupId>
<artifactId>das-boot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>das-boot</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<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>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Step 2: Here is your main application class:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
public class DasBootApplication {
public static void main(String[] args) {
SpringApplication.run(DasBootApplication.class, args);
}
}
or you can also stay with 1.4.2, just change java version to 1.6.
You should provide the class name in the run() method like below:
#SpringBootApplication
public static void main(String[] args) {
SpringApplication.run(App.class, args);
Sytem.out.print("Started");
}
And remove the below line from pom.xml
<start-class>com.boot.App</start-class>

Can find imported Spring beans from JAR, but not from WAR

My application consists of three Maven projects (I am omitting other modules)): FrameworkBase (JAR), FrameworkBaseImpl (JAR), FrameworkRestService (WAR).
FrameworkRestService is a plugin to FrameworkBaseImpl, drawing its class/interface definitions
from FrameworkBase, which FrameworkBaseImpl implements/extends. (I had to split FrameworkBase to avoid Maven curcular dependencies).
FrameworkBaseImpl has a main, which invokes Jetty, passing the FrameworkRestService war.
I am trying to have Spring inject a RestService instance as a field member in the (sole) instance of FrameworkMain class of FrameworkBaseImpl. My Spring points to FrameworkRestService class inside FrameworkRestService project as implementing the restService bean.
And here is the problem: Although Spring seems to find the imported framework_rest_service.context file in the WAR that contains the bean it fails injecting it (no bean named restService is found). But if I convert FrameworkRestService into a JAR - it magically succeeds! I am stuck for 2 days on it! Please help!
FrameworkBase 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>
<artifactId>framework.base</artifactId>
<packaging>jar</packaging>
<name>framework.base</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<parent>
<groupId>controlapps</groupId>
<artifactId>controlapp</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../controlapp/pom.xml</relativePath>
</parent>
</project>
FrameworkBase RestService:
package com.company.controlapps.framework.base;
public class RestService {
public FrameworkMain frameworkMain;
/* Setters for Spring */
public void setFrameworkMain(FrameworkMain frameworkMain)
{this.frameworkMain = frameworkMain;}
}
FrameworkBase FrameworkMain:
package com.company.controlapps.framework.base;
public interface FrameworkMain {
...
}
FrameworkBaseImpl 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>
<artifactId>framework.base.impl</artifactId>
<packaging>jar</packaging>
<name>framework.base.impl</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<parent>
<groupId>controlapps</groupId>
<artifactId>controlapp</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../controlapp/pom.xml</relativePath>
</parent>
<dependencies>
<dependency>
<groupId>controlapps</groupId>
<artifactId>framework.base</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>controlapps</groupId>
<artifactId>framework.restservice</artifactId>
<version>0.0.1-SNAPSHOT</version>
<scope>runtime</scope>
<type>war</type>
</dependency>
</dependencies>
</project>
FrameworkBaseImpl FrameworkMainImpl:
package com.company.controlapps.framework.base.impl;
public class FrameworkMainImpl implements FrameworkMain {
....
protected RestService restService;
public void setRestService(RestService restService)
{this.restService = restService;}
}
public static void main(String[] args) {
context = new ClassPathXmlApplicationContext(SPRING_CONTEXT_FILENAME);
FrameworkMainImpl frameworkMain = (FrameworkMainImpl) context.getBean("frameworkMain");
...
}
FrameworkBaseImpl spring context.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<import resource="classpath*:framework_restservice_context.xml"/>
<bean id="frameworkMain" class="com.company.controlapps.framework.base.impl.FrameworkMainImpl" >
<property name="restService" ref="restService" />
</bean>
</beans>
FrameworkRestService FrameworkRestService 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>
<artifactId>framework.restservice</artifactId>
<packaging>war</packaging>
<name>framework.restservice</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<jersey.version>1.12</jersey.version>
</properties>
<parent>
<groupId>controlapps</groupId>
<artifactId>controlapp</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../controlapp/pom.xml</relativePath>
</parent>
<dependencies>
<dependency>
<groupId>controlapps</groupId>
<artifactId>framework.base</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-server</artifactId>
<version>${jersey.version}</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-json</artifactId>
<version>${jersey.version}</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-server-linking</artifactId>
<version>${jersey.version}</version>
</dependency>
<dependency>
<groupId>com.sun.jersey.contribs</groupId>
<artifactId>jersey-spring</artifactId>
<version>${jersey.version}</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.5.4</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
</dependencies>
</project>
FrameworkRestService spring framework_restservice_context.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="restService" class="com.radware.controlapps.framework.restservice.FrameworkRestService">
<property name="frameworkMain" ref="frameworkMain" />
</bean>
</beans>
FrameworkRestService FrameworkRestService.java snippet:
package com.company.controlapps.framework.restservice;
#Path("myresource")
public class FrameworkRestService extends RestService {
...
#Context
UriInfo uriInfo;
#Context
Request request;
#GET
#Produces(MediaType.APPLICATION_JSON)
public MyResource getMyResource() {
...
frameworkMain.doSomething();
....
}
Sorry for the long question. :-)
It wasn't a class loader issue. I had the WAR and the JAR use the same class loader (of the JAR). Somebody commented that I shouldn't have tried to link a WAR and look for beans in there.
So instead of trying to solve the problem I simply bypassed it by canceling Spring dependencies between the JAR and the WAR. In the WAR I still needed a reference to the FrameworkMain singleton instantiated in the JAR.
I "hacked" this as follows: I added a class named Holder with public static methods get and set the FrameworkMain instance singleton. I call get from the WAR, and set from the jar. I could not add this to FrameworkMain, which is actually an interface in FrameworkBase jar (implemented by FrameworkMainImpl in the FrameworkBaseImpl jar).
Thanks.

Resources