Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

"Bean type int could not be found" error, how can I solve it?

I made a TempRequest class and registed as a bean

The code below is TempRequest class.

package com.wook.model.dto;

import org.springframework.stereotype.Component;

@Component
public class TempRequest {

    private int x;
    private int y;
    private String baseDate;
    private String tempKey; 
    
    public TempRequest(int x, int y, String baseDate, String tempKey) {
        super();
        this.x = x;
        this.y = y;
        this.baseDate = baseDate;
        this.tempKey = tempKey;
    }

    public String getTempKey() {
        return tempKey;
    }

    public void setTempKey(String tempKey) {
        this.tempKey = tempKey;
    }

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

    public String getBaseDate() {
        return baseDate;
    }

    public void setBaseDate(String baseDate) {
        this.baseDate = baseDate;
    }

    @Override
    public String toString() {
        return "TempRequest [x=" + x + ", y=" + y + ", baseDate=" + baseDate + ", tempKey=" + tempKey + "]";
    }
    
}

To use TempRequest class I Autowired at ReadWeatherController class

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

ReadWeatherController Class

package com.wook.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.wook.model.dto.TempRequest;

@RestController
public class ReadWeatherController {

    private TempRequest tr;

    public ReadWeatherController(TempRequest tr) {
        super();
        this.tr = tr;
    }


    //이제 여기다가 API를 만들면됨
    @GetMapping("/todayMaxMin")
    public String readWeather(
            @RequestParam(value="x")int x,
            @RequestParam(value="y")int y,
            @RequestParam(value="baseDate")String baseDate) {
        
        String tempKey = baseDate+String.valueOf(x)+String.valueOf(y);
        
        tr = new TempRequest(x,y,baseDate,tempKey);
        
        return tr.toString();
    }
    
}

And to use these beans I added a packges with using scanBasePackages in main class.
Application Class

package com.wook.run;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;

@EnableScheduling
@SpringBootApplication(scanBasePackages= {"com.wook.controller","com.wook.model","com.wook.service"})
public class Application {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        SpringApplication.run(Application.class, args);
    }

}

However with this code I get an error like this.


APPLICATION FAILED TO START


Description:

Parameter 0 of constructor in com.wook.model.dto.TempRequest required a bean of type ‘int’ that could not be found.

Action:

Consider defining a bean of type ‘int’ in your configuration.

How can I solve this problem?

>Solution :

The problem is that Spring is trying to create a bean for TempRequest (and the only constructor available requires 2 ints and 2 Strings). You actually don’t need TempRequest as a Spring-managed Bean, so remove @Component annotation:

public class TempRequest {
    (...)
}

Then you also need to remove TempRequest as a property in ReadWeatherController because you actually don’t need it:

@RestController
public class ReadWeatherController {

    //이제 여기다가 API를 만들면됨
    @GetMapping("/todayMaxMin")
    public String readWeather(
            @RequestParam(value="x") int x,
            @RequestParam(value="y") int y,
            @RequestParam(value="baseDate") String baseDate) {
        
        String tempKey = baseDate+String.valueOf(x) + String.valueOf(y);
        
        TempRequest tr = new TempRequest(x, y, baseDate, tempKey);
        
        return tr.toString();
    }
}

As a side note, please consider moving your main class Application to the package com.wook so that you avoid the need of adding all the packages in @SpringBootApplication(scanBasePackages= {"com.wook.controller","com.wook.model","com.wook.service"}):

package com.wook;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;

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

@SpringBootApplication encapsulates @Configuration, @EnableAutoConfiguration, and @ComponentScan annotations with their default attributes. The default value for @ComponentScan means that all the sub packages on the package the @ComponentScan is used are scanned. That is why it is usually a good practice to include the main class in the base package of the project.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading