Spring MVC Web Application: No Default Constructor Found

No default constructor found; nested exception is java.lang.NoSuchMethodException with Spring MVC?

You have to define no-args or default constructor if you are creating your own constructor.

You can read why default or no argument constructor is required here:

Why default or no argument constructor is important in Java class?

Spring MVC no default constructor found?

You should either define your beans in xml or annotate them, not both (if only to avoid errors like the one you're getting).

The problem here is that you're not autowiring constructor args, so spring doesn't know what to do with your controller. It knows it has to create a bean (@Controller annotation), but it doesn't know how (no default, nor autowired constructor).

You can try to do something like:

@Controller
public class CacheHandler {

private final CustomGxSessionIdCacheImpl gxSessionIdCache;

@Autowired
public CacheHandler(CustomGxSessionIdCacheImpl gxSessionIdCache) {
this.gxSessionIdCache = gxSessionIdCache;
}

and then in xml:

<bean id="gxSessionIdCache"
factory-bean="PcrfSimulator"
factory-method="getGxSessionIdCache"/>

So it will autowire constructor parameters.

Another option is to simply create default constructor and autowire gxSessionIdCache property.

Spring MVC web application: No default constructor found

Inside your controller method , you have used a parameter as array like below

@ModelAttribute("dslrs") DSLR dslrs[]

so use collection like list instead

@ModelAttribute("dslrs") ArrayList<DSLR> dslrs

Keep getting "No default constructor found" for controller

Since you have

@Autowired
private EventService service;

You do not need the constructor with parameter. You can remove it,

or add @Autowired to it and remove @Autowired from the property service

private EventService service;

@Autowired
public EventController(EventService service) {
this.service = service;
}

Spring: No default constructor found

As the error is impossible with the source code you shared with us, my guess would be that your server contains old class (i.e. the source code we see is not being used).

I suggest to clean the project and also Tomcat installation in your IDE.

On project in Eclipse (if that is your IDE):

Menu Project => Clean ...

On Tomcat in Eclipse (if that is your IDE):

Right click on the server in Servers view => Clean ...

Error creating bean with name 'application', No default constructor found; nested exception is java.lang.NoSuchMethodException

You can't autowire into main Spring Boot class. You can inject dependencies needed for CommandLineRunner as parameters of method annotated with @Bean and of course remove constructor injection for main class:

@SpringBootApplication
public class Application {
private static final Logger log = LoggerFactory.getLogger(Application.class);

public static void main (String[] args) {
SpringApplication.run(Application.class, args);
}

@Bean
public CommandLineRunner app(ApplicationContext applicationContext,
MessagingService messagingService,
Parser parser) {
return args -> {
Locale defaultLocale = Locale.getDefault();
Locale.setDefault(defaultLocale);
log.info("Using MessagingService: " + messagingService.getMyMessageCode());

parser.parse();
};
}
}

EDIT:
Correct context configuration after edit:

@SpringBootApplication
public class Application {

private static final Logger log = LoggerFactory.getLogger(Application.class);

public Application() {}

public static void main (String[] args) {
SpringApplication.run(Application.class, args);
}

@Bean
public CommandLineRunner app(MessagingService messagingService, Parser parser) {
return args -> {
Locale defaultLocale = Locale.getDefault();
Locale.setDefault(defaultLocale);
log.info("Using MessagingService: " + messagingService.getMyMessageCode());

parser.parse();
};
}
}


Related Topics



Leave a reply



Submit