私はBaseController持っている:抽象的なBaseControllerで共通の '/ ping' APIリソースを作成するには?
@Controller
public abstract class BaseController {
/**
* The Logger for this class hierarchy
*/
protected Logger logger = LoggerFactory.getLogger(this.getClass());
@RequestMapping(value = "/ping", produces = MediaType.APPLICATION_JSON_VALUE)
public ServiceResponse<?> ping() {
ServiceResponse<Object> serviceResponse = new ServiceResponse<>(ResponseStatus.SUCCESS);
return serviceResponse;
}
}
そして、このように、このBaseControllerを拡張するいくつかの他のコントローラ:
@RestController
@RequestMapping(value= "/admin")
public class AdminController extends BaseController {
@RequestMapping(
value = "/all",
method = RequestMethod.GET,
produces = MediaType.APPLICATION_JSON_VALUE)
public ServiceResponse<List<Account>> getAll() {
ServiceResponse<List<Account>> serviceResponse = accountService.getAll();
return serviceResponse;
}
}
しかし、私はサーバーの起動時にエラーが出るとすぐに私がpingを移動すると()リソースBaseController:
を2016-04-23 21:31:39.862 ERROR 2335 --- [ main] o.s.boot.SpringApplication : Application startup failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'requestMappingHandlerMapping' defined in class path resource [org/springframework/boot/autoconfigure/web/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]: Invocation of init method failed; nested exception is java.lang.IllegalStateException: Ambiguous mapping. Cannot map 'clientController' method
public ca.gatin.api.response.ServiceResponse<?> ca.gatin.api.controller.BaseController.ping()
to {[/admin/ping],produces=[application/json]}: There is already 'adminController' bean method
public ca.gatin.api.response.ServiceResponse<?> ca.gatin.api.controller.BaseController.ping() mapped.
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1578) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:545) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:772) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:839) ~[spring-context-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:538) ~[spring-context-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118) ~[spring-boot-1.3.3.RELEASE.jar:1.3.3.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:766) [spring-boot-1.3.3.RELEASE.jar:1.3.3.RELEASE]
at org.springframework.boot.SpringApplication.createAndRefreshContext(SpringApplication.java:361) [spring-boot-1.3.3.RELEASE.jar:1.3.3.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:307) [spring-boot-1.3.3.RELEASE.jar:1.3.3.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1191) [spring-boot-1.3.3.RELEASE.jar:1.3.3.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1180) [spring-boot-1.3.3.RELEASE.jar:1.3.3.RELEASE]
at ca.gatin.TodoAppApplication.main(TodoAppApplication.java:10) [classes/:na]
複数のコントローラでping()リソースを複製する必要はありません。どうすればBaseControllerに正しく移動できますか?
BaseController( 'admin/ping'など)を拡張するすべてのコントローラのpingマッピングを行いますか?または、作成するBaseControllerサブクラスの数に関係なく、1つの '/ ping'マッピングですか? –
私はpingメソッドを/ pingのようなrequestMappingのルートに入れたいです。 –