Controller not getting mapped - spring-boot

My controller is not getting mapped in the console.
My main class Application
package com.ruchi.web.sbfirst;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
public class SbFirstApplication {
public static void main(String[] args) {
SpringApplication.run(SbFirstApplication.class, args);
}
}
LoginController
package com.ruchi.web.sbfirst.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
#Controller
public class LoginController {
#RequestMapping("/login")
public String loginMessage() {
return "Hello";
}
}

Add #RestController annotation before the LoginController class
#RestController
public class LoginController {
#RequestMapping("/login")
public String loginMessage() {
return "Hello";
}
}
Reason you are using spring boot and also if you want to use #Controller annotation enable WebMVC.

click on the your project and go to the gradle or maven (which ever you have used) and refresh it then built your project and now re-run it this time it will not disappoint your :)

Related

Aspect from External Jar is getting executed

I have an aspect written in a project called connector and generated a jar for that project. I have integrated that jar into another spring boot application as a Gradle dependency. This aspect is not working.
JAR FILE CODE
TrackExecutionTime.java
package a.b.aspect;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
#Target(ElementType.METHOD)
#Retention(RetentionPolicy.RUNTIME)
public #interface TrackExecutionTime {}
ConnectorAOPConfig.java
package a.b.aspect;
#Aspect
#Component
public class ConnectorAOPConfig {
public ConnectorAOPConfig() {
}
#Around("#annotation(a.b.aspect.TrackExecutionTime)")
public Object logExecutionTime(ProceedingJoinPoint joinPoint) throws Throwable {
System.out.println("Inside Aspect");
return joinPoint.proceed();
}
}
MAIN CODE
AOPConfig.java
package a.c.config;
// Imports here
#Configuration
#EnableAspectJAutoProxy
#ComponentScan(basePackages = {"a.b.aspect"})
public class AOPConfig {
#Bean
public ConnectorAOPConfig aopHandler()
{
return new ConnectorAOPConfig();
}
}
Code targeted by aspect:
#TrackExecutionTime
public static ApplicationDTO fetchApplicationByName(String name) {
//code
return application;
}
Note: I added that annotation in the connector jar only. Still it is not getting triggered.

I am trying to get NoUniqueBeanDefinitionException but i am not getting it any clue why i am not getting

I am trying to get NoUniqueBeanDefinitionException but i am not getting it any clue why i am not getting
package com.example.demo;
public interface IDateGen {
}
package com.example.demo;
import org.springframework.stereotype.Service;
#Service
public class DateGen implements IDateGen {
}
package com.example.demo;
import org.springframework.stereotype.Service;
#Service
public class DateGen2 implements IDateGen {
}
package com.example.demo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
#Component
public class DateGenUtil {
#Autowired
private IDateGen dateGen;
public IDateGen getDateGen() {
return dateGen;
}
public void setDateGen(IDateGen dateGen) {
this.dateGen = dateGen;
}
}
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
#SpringBootApplication
public class Demo3Application {
public static void main(String[] args) {
ApplicationContext appContext = SpringApplication.run(Demo3Application.class, args);
DateGenUtil util = appContext.getBean(DateGenUtil.class);
System.out.println(util.getDateGen());
}
}
When i run the main method i getting
com.example.demo.DateGen#6075b2d3
Can anyone tell why i am not getting NoUniqueBeanDefinitionException ? Thanks in advance
The dependency injection first checks for autoWiring by type and then autowire by name, wen it looks for dateGen dependency in class DateGenUtil it checks by type then it is getting two Objects, then again it tries to do autoWiringByName it gets the one object
if we rename the variable like in the below code it gives the exception, hope anyone can confirm this
#Component
public class DateGenUtil {
#Autowired
//private IDateGen dateGen;
private IDateGen date;
}

Spring Boot did not Autowiring

Can someone tell me please what I'm doing wrong, I'm trying to develop a little application but I just simply can't Autowire a class in the controller, I have been researching and I Know that for Autowire works, my class must be in a child package of my #SpringBootApplication annotation, or I should specify in #ComponentScan annotation the package of my class, but no matters what I do I still getting the same error:
This is my project structure:
package net.spring.auditoria;
import org.springframework.boot.SpringApplication;T
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
public class AuditoriaApplication {
public static void main(String[] args) {
SpringApplication.run(AuditoriaApplication.class, args);
}
}
Controller:
package net.spring.auditoria.bll;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
#Controller
public class AuditoriaController {
#Autowired
private AuditoriaManagement am;
private final Logger LOGGER = LoggerFactory.getLogger(AuditoriaController.class);
#GetMapping("/auditar")
public String auditar() {
LOGGER.info("auditar()");
return "main";
}
}
https://github.com/jlpm-mex/auditoria
Because AuditoriaManagement is not a Spring bean or component.

Spring #Transactional annonation not working

I am facing an issue when I use a #Transactional annotation in a class which is extended by Service class. I know that Spring does Transaction Management only when a method is invoked by a Proxy but my problem is because of this annotation I am not able to get another Service class's property's value when accessing by object directly (property is public), but same I am able to fetch through getter method.
Controller
package com.springbootdemo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
#RestController
public class DemoController {
#Autowired
Service1 service1;
#GetMapping("/")
public void index() {
service1.doSomething();
}
}
Service1
package com.springbootdemo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
#Component
public class Service1 {
#Autowired
Service2 service2;
public void doSomething(){
service2.setSname("Hello");
System.out.println("sname==> "+service2.sname);
System.out.println("getSname==>"+service2.getSname());
}
}
UtilBean
package com.springbootdemo;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
#Component
public class UtilBean {
#Transactional(propagation = Propagation.REQUIRES_NEW)
public String appendString(String s1) {
return " Hi "+ s1;
}
}
Service2
package com.springbootdemo;
import org.springframework.stereotype.Component;
#Component
public class Service2 extends UtilBean{
public String sname;
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
}
application.properties
management.endpoints.web.exposure.include=*
#management.endpoints.web.exposure.exclude=loggers
#security.user.name=admin
#security.user.password=admin
spring.datasource.url=jdbc:mysql://192.168.0.10:3306/aswaraj?zeroDateTimeBehavior=convertToNull&useSSL=false
spring.datasource.username=root
spring.datasource.password=test
spring.datasource.platform=mysql
spring.session.store-type=none
security.basic.enabled=false
Output
sname==> null
getSname==>Hello

How to inject parent primitive type property through child class constructor when componentscan is enabled

I have a parent class Car & a sub class Axio. So, i'm trying to pass an argument through super("Axio") within child constructor to constructor parameter in the parent class which then assign the value into a property defined within the parent class. When i try executing the application in spring boot it throws me an exception stating
Description:
Field car in com.test.efshop.controller.HelloController required a bean of type 'com.test.efshop.Axio' that could not be found.
Action:
Consider defining a bean of type 'com.test.efshop.Axio' in your configuration.
Can anyone please tell me how to achieve this in spring boot?. My Code is as below,
// Car class
package com.test.efshop;
public class Car {
private String carName;
public String getCarName() {
return carName;
}
public void setCarName(String carName) {
this.carName = carName;
}
public Car(String carName) {
this.carName = carName;
}
public String print() {
return "Car name is : "+carName;
}
}
//sub class of car class which is Axio
package com.test.efshop;
public class Axio extends Car{
public Axio() {
super("Axio");
}
}
//main method
package com.test.efshop;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;
#SpringBootApplication
public class DemoApplication extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
//controller class
package com.test.efshop.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import com.test.efshop.Axio;
import com.test.efshop.Engine;
#Controller
public class HelloController {
#Autowired
private Axio car;
#RequestMapping(value = "/hello")
public ModelAndView print() {
return new ModelAndView("index");
}
//This is the method which i used to return the value of Car class
#RequestMapping(value = "/hello2")
#ResponseBody
public String print2() {
return car.print();
}
}
As pvpkiran commented, you can't #Autowire a class if it's not a Spring bean.
Option a) you convert the Axio class into a service or component.
#Component
public class Axio extends Car {
public Axio() {
super("Axio");
}
}
Option b) you define a bean of type Axio.
#SpringBootApplication
public class DemoApplication extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
#Bean
public Axio myAxioBean() {
return new Axio();
}
}

Resources