java - Spring Boot 显示对索引的 http 请求,但不显示任何其他映射

标签 java spring spring-boot http gradle

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

使用 @ComponentScanMain

@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/

相关文章:

java - 具有非 SpringBoot 托管构造函数参数的 Autowiring 类

java - 面板为什么不绘制(paint)?

java - 如何创建一个带有数组的类,然后将值传递给程序?

spring - 在 Websphere 上使用 Aspectj

java - 如何覆盖 Spring Boot 2.0.x 中的默认连接池限制

java - 如何在 Spring Boot 中使用 spring-security.xml 中的配置?

java - java中的断言是如何工作的?

java - 是否使引用变量易变,使其所有字段在 java 中也易变?

java - 带有对象的通用请求接收 double 值而不是整数值

java - spring boot2 embedded tomcat的root文件如何修改