TransWikia.com

@RequestMapping can not use a static final constant in parent class which is not in same package with subclass

Stack Overflow Asked on November 15, 2021

when @RequestMapping used a static final constan in parent class which is not in same package with subClass class,the idea give me a error like

‘V1_BASE_URL’ has protected access in ‘XXXXXXXX.BaseController’

so,i dont know why,the next is my code.

this is the parent class

package com.controller;
public class BaseController {

protected static final String V1_BASE_URL = "/crm/v1/";

}

this is subclass,and it is not in the same package with BaseController

package com.controller.corpus;
@RequestMapping(V1_BASE_URL+"/crm/v1/corpus_subset")
public class CorpusSubsetController extends BaseController {

}

error image

i dont find some useful answer can deal my problem,but i found if the subclass and father in the same package,the annotation in classname can use static constant,but if the subclass not in the same package with father, the annotation can not use,so my question is the annotation in classname belongs who?if the annotation in a class which one in the package A,so the annotation belongs the package A??

One Answer

It's because you are trying to access V1_BASE_URL in the annotation which is outside the class scope. If you access it like this you won't have any compilation errors for example :

public class CorpusSubsetController extends BaseController {
    public void test() {
        String test = V1_BASE_URL;
    }
}

Answered by Fabien on November 15, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP