This application has no explicit mapping for /error, so you are seeing this as a fallback.

lys2018年05月04日 0条评论

This application has no explicit mapping for /error, so you are seeing this

应该使用@SpringBootApplication

package com;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * Hello world!
 *
 */
@Controller
@EnableAutoConfiguration
@SpringBootApplication
public class App 
{
	@RequestMapping("/")
	@ResponseBody 
	public String index(){
		return "hahahaha";
	}
    public static void main( String[] args )
    {
    	 SpringApplication.run(App.class, args);
    }
}

其他控制器

package com;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@Controller
public class TestController {

    @RequestMapping("test")
    @ResponseBody 
    public String test(){
        return "test";
    }
}

原因是:

如果使用@Controller和@EnableAutoConfiguration 注解还应该再加上一个注解:@ComponentScan  就可以了。@Controller和@EnableAutoConfiguration没有扫描注解的功能,而@ComponentScan是

 springboot专门用来扫描@Component, @Service, @Repository, @Controller等注解的注解

 

总结:

使用springboot启动类配置扫描的两种注解配置方式:

1、@Controller

   @EnableAutoConfiguration

   @ComponentScan

2、@SpringBootApplication

@SpringBootApplication注解等价于@Configuration, @EnableAutoConfiguration and @ComponentScan

 

另外application.java(启动类)也应该按照官方的建议放在root目录下,这样才能扫描到Service和dao,不然还会引起,扫描不到注解的问题。


文章参考:

https://www.cnblogs.com/oskyhg/p/6683629.html