Spring Boot 显示对索引路径的 HTTP 请求,但不显示任何其他映射。
我创建了一个在 localhost:8060 上运行的 spring boot 应用程序。问题是,当我在浏览器上运行 localhost:8060 时,它可以正常工作,但是当我运行任何其他请求时,它会显示“出现意外错误(type=Forbidden,status=403)”。 拒绝访问'。在日志中它说它找到了正确的方法,即它映射正确但仍然抛出这个错误。
问候语.java
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* Main application to launch spring boot.
*/
@RestController
public class Greetings {
/**
* Main application to launch spring boot.
*/
@RequestMapping("/")
public String index() {
return "Greetings from Spring Boot!";
}
@RequestMapping("/testing123")
public String indextest() {
return "Testing";
}
}
服务器应用
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.scheduling.annotation.EnableAsync;
/**
* Main application to launch spring boot.
*/
@SpringBootApplication
@EnableAutoConfiguration
@EnableAsync
public class ServerApplication {
private static Logger logger = LoggerFactory.getLogger(ServerApplication.class);
/**
* Start the Spring Boot application.
*
* @param args command line arguments
*/
public static void main(String[] args) {
ApplicationContext context = SpringApplication.run(ServerApplication.class, args);
logger.info("Sample Application started with context {}", context.getDisplayName());
}
}
应用程序属性
# Initialize feature service.
# This flag should be set ONLY in environments where ss-backend isn't running - normally - ss-backend is set as the
# leader so that it can control (via UI) setting any and all features.
feature.service.is.leader=true
logging.level.org.springframework.web: TRACE
security.ignored=/**
security.basic.enable: false
spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration
所以它会显示“来自 Spring Boot 的问候!”当我转到 localhost:8060 但在 localhost:8060/testing123 上抛出错误
最佳答案
因为你主类ServerApplication
在另一个包中,它是 spring boot 应用程序的基础包。
但是 Controller Greetings
在不同的包中,不是主类的子包,默认情况下,spring boot 应用程序加载所有用 sub packages
中的任何构造型注释注释的类基础包的 spring
bean 入ApplicationContext
使用 @ComponentScan
在 Main
类
@SpringBootApplication
@EnableAutoConfiguration
@EnableAsync
@ComponentScan({"com.vmware.skyscraper", "com.skyscraper.vdisizer"})
public class ServerApplication {
private static Logger logger = LoggerFactory.getLogger(ServerApplication.class);
/**
* Start the Spring Boot application.
*
* @param args command line arguments
*/
public static void main(String[] args) {
ApplicationContext context = SpringApplication.run(ServerApplication.class, args);
logger.info("Sample Application started with context {}", context.getDisplayName());
}
}
关于java - Spring Boot 显示对索引的 http 请求,但不显示任何其他映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55944060/