简体   繁体   中英

Spring Boot can't access REST Controller

I have a simple problem - SpringBootApplication doesn't see my controller - what's more weird - only one of three.

I have UserController, WalletController and DashboardController - this one is not visible for my application.

What I have already done is:

  1. Every package with controller is under the main package, where my SpringBootApplication.class is,
  2. I tried annotate main SpringBootApplication.class with @ComponentScan both with basePackages and basePackageClasses,
  3. There is no other beans - which should be annotated @Component , I removed them and moved methods to my DashboardService.class

This is my controller, which is not visible: DashboardController

And this is my package structure(seems to be right): Package Structure

Thank You for help!

EDIT:

It might be important, that I use the third-party api to get the data I need

In that methods I use url :

    private String getNbpJson(String url) {
        return new RestTemplate().getForObject(url, String.class);
    }

    private CurrentRateDTO getCurrentExchangeRate(String json) throws JsonProcessingException {
        ObjectMapper mapper = new ObjectMapper();

        JsonNode node = mapper.readTree(json);

        String code = node.get("code").toString();
        String date = node.get("rates").get(0).get("effectiveDate").toString();

        double bid = node.get("rates").get(0).get("bid").asDouble();
        double ask = node.get("rates").get(0).get("ask").asDouble();

        return new CurrentRateDTO(code, date, bid, ask);
    }

And then in ExchangeService this is my url

@Service
public class ExchangeRateService implements IExchangeRateService {

    private static final Logger logger = LoggerFactory.getLogger(ExchangeRateService.class);

    private String NBP_CURRENT_RATE_URL = "http://api.nbp.pl/api/exchangerates/rates/c/eur/2020-12-11/?format=json";

What's more... when I move methods from DashboardController to WalletController (which works)

Another thing that I have found out is that only methods, which make use of the third-party api don't work. Basically, I retrieve data from the url above - I get the specific fields, create an objects with filled fields.

May it be a problem with retrieving data from the third-party and then implementing it in my app?

I have no more ideas for now...

Maybe your controller is registered but you type a slightly different url. Try this property logging.level.org.springframework.web.servlet.mvc.method.annotation: TRACE

and check on application startup if the controller is registered under some other url.

Try to add this to your controller

@RestController()   
@RequestMapping("exchangerates")

Okay, I probably found out the problem... Before I have 3 controllers:

@RestController("/api")
public class UserController {}

@RestController("/wallets")
public class WalletController {}

@RestController("/exchangerates")
public class DashboardController {}

I changed the above to

@RestController("/api")
public class UserController {}

@RestController
public class WalletController {}

@RestController
public class DashboardController {}

So, basically I removed base?endpoint? for each controller and now it works... It seems like basic endpoints in three RestControllers is too much and the third one is not available, but I don't know is it truth (I bet that it's not).

Why that happened then, can anybody explain that behaviour of controllers? Thank You for explanantion.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM