【问题标题】:Spring boot on Google appengine standard - REST APIGoogle appengine 标准上的 Spring Boot - REST API
【发布时间】:2019-02-11 08:17:50
【问题描述】:

我正在尝试通过命令 mvn appengine:run 在本地 appengine 模拟器上运行我的代码并且没有错误,只是它找不到任何 RestController(例如:No file找到:/setiaalam/amenities)。

另外,启动没有显示没有 Spring Boot 标志,所以我怀疑我需要为它指定 servlet init?它在我自己的 Apache Tomcat Eclipse 环境中运行良好,但这只有在我要“运行”主类时才有效。

更具体地说,我没有创建自定义 servlet,我只想将其迁移到 Google Cloud AppEngine Standard - 虽然没有错误,但根本没有 Spring Boot 启动徽标。尝试使用 Postmen 访问任何在本地工作的 GET API 总是返回 404。尝试从以前的 Apache Tomcat localhost 访问它时没有问题。

是的,我在这里遵循 github 指南: Link to Github for Spring boot Google Appengine Standard

它没有提到任何关于修改 web.xml 的内容。

我在这里遗漏了什么?

代码(主应用):

@SpringBootApplication
@EnableJpaAuditing
public class SetiaAlamApplication{

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

}

代码(控制器的1):

@RestController
@RequestMapping("/setiaalam")
public class AmenityController {

@Autowired
AmenityDAO amenityDAO;

//service to get all amenities
@GetMapping("/amenities")
public List<Amenity> getAllAmenities(){
    return amenityDAO.findAll();
}

代码(需要的SpringBootServletInitializer):

public class ServletInitializer extends SpringBootServletInitializer {

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

application.properties:

# Spring DATASOURCE (DataSourceAutoConfiguration & DataSourceProperties)
spring.datasource.url = jdbc:mysql://ipaddress:3306/setiaalam? 
useSSL=false&autoReconnect=true&failOverReadOnly=false&maxReconnects=10
spring.datasource.username = hidden
spring.datasource.password = hidden

# Hibernate Properties
# The SQL dialect makes Hibernate generate better SQL for the chosen 
database
spring.jpa.properties.hibernate.dialect = 
org.hibernate.dialect.MySQL5Dialect

# Hibernate ddl auto (create, create-drop, validate, update)
spring.jpa.hibernate.ddl-auto = update

【问题讨论】:

  • 这可能是一个包问题,通常你需要告诉 spring 在哪里寻找控制器。你在哪里找到你的控制器?您可能需要在 SetiaAlamApplication 类中使用 @ComponentScan 注释
  • 嗨@ItaiSoudry,感谢您的帮助。根据 Spring 引导实践的建议,所有控制器都位于 SetiaAlamApplication 类的子文件夹中......并且它只需要在不同的包中使用 ComponentScan 注释。另外,只是好奇,什么时候调用 ServletInitializer 因为我只是按照我在 github 中提供的链接的示例进行操作,无论是从 web.xml 还是 pom.xml 都没有使用它。
  • 我实际上从未使用过 servlet 初始化程序。运行 SetiaAlamApplication 主函数时的控制台输出是什么?
  • Google App Engine 指南建议我使用 Servlet Initializer。你是什​​么意思?您的意思是打开另一个控制台并运行 SetiaAlamApplication 主函数?顺便说一句,如果要完全按照指南在本地运行,命令是 mvn appengine:run。但是,如果我使用另一个命令 mvn spring-boot:run 运行,我可以看到 Spring boot 徽标已启动,但会出现单独的错误(由于缺少 ServletWebServerFactory bean,无法启动 ServletWebServerApplicationContext)。
  • 好的,一些更新。我遵循 Google 的另一个示例,使用 Google App Engine 的 Eclipse 插件。还是同样的404问题。link

标签: java rest spring-boot google-app-engine


【解决方案1】:

好的,问题已解决。我该如何解决?

第 1 步 下载最新的 Eclipse。

第 2 步 接下来是从 Eclipse Market 添加 Spring Tool 功能。请注意,如果找不到 Eclipse Market,则需要添加第一个。简单的谷歌会指导你如何去做。

第 3 步 然后添加 Google Cloud Tool(来自 Eclipse Market)。

第 4 步 添加后,创建一个新的 Spring 项目(通过 Eclipse,之前我没有使用 spring.io)。

第 5 步 根据生成的单个主应用程序,根据 Google App Engine Standard 的 Google 指南更改 pom.xml 上所需的配置。

第 6 步 完成后,创建一个简单的 1 控制器,它只返回一个字符串文本。在本地部署 - 当然是在 Jetty 上,一旦工作,将所有类一个一个地移动到这个新项目中。

重要提示 当然,如果你的项目像我这样需要更多额外的依赖,比如 Google API、Hibernate,你需要手动将它添加到 pom.xml 中。 此外,您肯定会遇到来自 Eclipse 的“无法更改动态 Web 模块方面”的问题,我遵循将项目文件 org.eclipse.wst.common.project.facet.core.xml 更改为 3.0 的简单指南jst.web。

谢谢

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-04-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-21
    • 2018-04-04
    • 2021-08-28
    • 2019-03-11
    相关资源
    最近更新 更多