gpt4 book ai didi

java - 无法使用 Spring Boot App 访问 RESTful 服务

转载 作者:行者123 更新时间:2023-11-29 08:27:42 25 4
gpt4 key购买 nike

我正在尝试在 Wildfly 13.0.0 上运行一个简单的 Spring Boot 应用程序,但在尝试访问任何 REST url 时我总是收到 404 错误。

这是我的应用类:

package com.application.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@SpringBootApplication
public class ExampleApp extends SpringBootServletInitializer{

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

@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(applicationClass);
}

private static Class<ExampleApp> applicationClass = ExampleApp.class;


}

@RestController
class HelloController {

@RequestMapping("/hello/{name}")
String hello(@PathVariable String name) {

return "Hi " + name + " !";

}
}

这是我的 pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.application</groupId>
<artifactId>example</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>

<name>example</name>
<description>Demo project for Spring Boot</description>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>8.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>

</dependencies>

<build>
<finalName>application-example</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

.war 已正确部署,如果我转到 localhost:8080/application-example/index.html,我可以访问我创建的 html 索引页面但如果我尝试 localhost:8080/application-example/hellolocalhost:8080/application-example/hello/myName甚至最后带有 0.0.1 快照的原始名称我总是得到 404。

当然,这也发生在内部包内的 RestController 类中,但在上面的示例中,我们在同一个包中,所以我认为这不是可见性/扫描问题。

这是我请求的 application.properties:

spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.properties.hibernate.globally_quoted_identifiers=true

spring.datasource.url=jdbc:mysql://localhost:3306/example
spring.datasource.username=root@localhost
spring.datasource.password=root
spring.datasource.driverClassName=com.mysql.jdbc.Driver

server.servlet.contextPath=/application-example

我错过了什么吗?如果您需要更多详细信息,请询问。

最佳答案

Spring Docs 中所述,您缺少以下内容:

A popular subject is that many people still wish to generate WAR files to be deployed inside containers. Both of these plugins support that as well. Essentially, you have to reconfigure your project to produce a WAR file and declare the embedded container dependencies as "provided". This ensures that the relevant embedded container dependencies aren’t included in the WAR file.

To build a war file that is both executable and deployable into an external container, you need to mark the embedded container dependencies as “provided”, as shown in the following example:

    <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>

注意:您不需要排除项。

更新按照这个工作 projectgithub 上为 spring-boot with wildfly 比较缺少的配置。

关于java - 无法使用 Spring Boot App 访问 RESTful 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51168867/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com