简体   繁体   中英

Spring Boot REST Controller doesn't work after setting attribute with custom xml bean

I am learning Spring framework (more generally Java EE).

I like the feature of passing the configuration using xml files. I started by the this example and it worked fine.

The only problem is that once I add my custom xml configuration with beans to set the attribute value inside the controller it doesn't work anymore, in the server log file it says Caused by: java.lang.IllegalStateException: Ambiguous mapping found. Cannot map 'com.example.controller.FirstController#0' bean method (...) Caused by: java.lang.IllegalStateException: Ambiguous mapping found. Cannot map 'com.example.controller.FirstController#0' bean method (...) then it lists all the methods in the controller exactly like if I defined multiple methods with identical RequestMapping (which is not the case).

I wanted to set a single attribute, but it seems that because of that the entire autoconfiguration doesn't work anymore.

Before

Main class

@Configuration
@ComponentScan
@EnableAutoConfiguration
public class MainApplication {

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

Controller class

@RestController
@RequestMapping("first")
public class FirstController {
    protected final Logger log = LoggerFactory.getLogger(getClass());

    @RequestMapping("test")
    public String test() {
        log.info("Test");
        return "OK";
    }
}

After

Main class

@Configuration
@ComponentScan
@EnableAutoConfiguration
@ImportResource("classpath:config.xml")
public class MainApplication {

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

Controller class

@RestController
@RequestMapping("first")
public class FirstController {
    protected final Logger log = LoggerFactory.getLogger(getClass());

    private String testingbean;
    public void setTestingbean(String testingbean) {
        this.testingbean = testingbean;
    }

    @RequestMapping("test")
    public String test() {
        log.info("Test");
        return "OK";
    }

    @RequestMapping("beantest")
    public String testBeans() {
        return testingbean;
    }
}

Config.xml file

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <!-- test bean -->
    <bean class="com.example.controller.FirstController">
        <property name="testingbean" value="works"/>
    </bean>
</beans>

In the Before version after accessing /first/test it returned OK , now I get blank page and Ambiguous mapping found error in the log file.

Could someone explain to me how to mix Spring Boot autoconfiguration with custom defined beans?

  1. I would also recommend to use property files to externalize configuration if possible.

EDIT: Spring boot provides fine documentation on that topic .

  1. The problem can be the combination of XML configuration and default component scan - the same bean can be defined twice. If this is the case consider "manual" exclude via http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/context/annotation/ComponentScan.html#excludeFilters--

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