【发布时间】: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