我正在尝试在
上启动HelloWorld应用程序如果我将它作为Spring Boot应用程序运行,但是ComponentScan
没有看到我的@RestController
bean,它的效果非常好。
我使用ServletInitializer
我可以从控制台输出和Intellij IDEA调试模式进入它。 Spring看到应用程序bean而不是控制器。
package test.gcp.hello;
...
public class ServletInitializer extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(DtmsApiGcpApplication.class);
}
}
所以我有空src/main/webapp/WEB-INF/web.xml
:
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="3.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
</web-app>
最小src/main/webapp/WEB-INF/appengine-web.xml
:
<?xml version="1.0" encoding="utf-8"?>
<appengine-web-app xmlns="http://appengine.google.com/ns/1.0">
<version>0.0.1</version>
<threadsafe>true</threadsafe>
<runtime>java8</runtime>
</appengine-web-app>
My Spring Boot Application类位于包层次结构的根目录中,因此Spring组件扫描程序必须找到所有@Components
。
package test.gcp.hello;
...
@SpringBootApplication
public class DtmsApiGcpApplication {
public static void main(String[] args) {
System.out.println("#######");
SpringApplication.run(DtmsApiGcpApplication.class, args);
}
}
这是控制器:
package test.gcp.hello.controllers;
...
@RestController
public class NodeController {
@GetMapping(value = "/api/node", produces = "application/json;charset=UTF-8")
public Mono<Node> home() {
return Mono.just(new Node("alias", "The Alias Node"));
}
}
的build.gradle:
buildscript {
ext {
springBootVersion = '2.0.0.M5'
}
repositories {
mavenCentral()
maven { url "https://repo.spring.io/snapshot" }
maven { url "https://repo.spring.io/milestone" }
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
classpath 'com.google.cloud.tools:appengine-gradle-plugin:+'
}
}
apply plugin: 'java'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
apply plugin: 'war'
apply plugin: "com.google.cloud.tools.appengine-standard"
group = 'eu.datatile.dtms.gcp'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8
targetCompatibility = 1.8
repositories {
mavenCentral()
maven { url "https://repo.spring.io/snapshot" }
maven { url "https://repo.spring.io/milestone" }
}
configurations {
providedRuntime
}
dependencies {
compile('org.springframework.boot:spring-boot-starter-webflux')
providedCompile 'com.google.appengine:appengine:+'
providedCompile 'javax.servlet:javax.servlet-api:3.1.0'
testCompile('org.springframework.boot:spring-boot-starter-test')
}
appengine {
tools {
cloudSdkHome = "path/to/google-cloud-sdk"
}
run {
port = 8080
}
deploy {
stopPreviousVersion = true
promote = true
}
}
Google App Engine Standard Local Server
的控制台输出:
Starting the App Engine local development server...
2017-11-03 19:35:24.932:INFO::main: Logging initialized @567ms
Connected to server
Nov 03, 2017 5:35:25 PM com.google.appengine.tools.development.IsolatedAppClassLoader checkWorkingDirectory
WARNING: Your working directory, (/Applications/IntelliJ IDEA.app/Contents/bin) is not equal to your
web application root (/Users/abc/dev/src/dtms/gcp/dtms-api-gcp/build/libs/exploded/dtms-api-gcp-0.0.1-SNAPSHOT.war)
You will not be able to access files from your working directory on the production server.
2017-11-03 19:35:25.291:INFO:oejs.Server:main: jetty-9.3.18.v20170406
2017-11-03 19:35:26.042:INFO:oeja.AnnotationConfiguration:main: Scanning elapsed time=540ms
Nov 03, 2017 5:35:26 PM com.google.appengine.tools.development.ApiProxyLocalImpl log
INFO: javax.servlet.ServletContext log: 2 Spring WebApplicationInitializers detected on classpath
...
17:35:26.171 [main] DEBUG org.springframework.web.context.support.StandardServletEnvironment - Replacing PropertySource 'servletContextInitParams' with 'servletContextInitParams'
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.0.0.M5)
2017-11-03 17:35:26.649 INFO 10294 --- [ main] e.d.dtms.gcp.api.ServletInitializer : Starting ServletInitializer on alexanders-mbp with PID 10294 (/Users/abc/dev/src/dtms/gcp/dtms-api-gcp/build/libs/exploded/dtms-api-gcp-0.0.1-SNAPSHOT.war/WEB-INF/classes started by abc in /Applications/IntelliJ IDEA.app/Contents/bin)
2017-11-03 17:35:26.650 INFO 10294 --- [ main] e.d.dtms.gcp.api.ServletInitializer : No active profile set, falling back to default profiles: default
2017-11-03 17:35:26.711 INFO 10294 --- [ main] ConfigServletWebServerApplicationContext : Refreshing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@573906eb: startup date [Fri Nov 03 17:35:26 UTC 2017]; root of context hierarchy
2017-11-03 17:35:27.316 INFO 10294 --- [ main] c.g.a.t.development.ApiProxyLocalImpl : javax.servlet.ServletContext log: Initializing Spring embedded WebApplicationContext
2017-11-03 17:35:27.316 INFO 10294 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 605 ms
2017-11-03 17:35:27.499 INFO 10294 --- [ main] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'errorPageFilter' to: [/*]
2017-11-03 17:35:27.499 INFO 10294 --- [ main] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*]
2017-11-03 17:35:27.826 INFO 10294 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2017-11-03 17:35:27.838 INFO 10294 --- [ main] e.d.dtms.gcp.api.ServletInitializer : Started ServletInitializer in 1.658 seconds (JVM running for 3.474)
2017-11-03 19:35:28.005:INFO:oejsh.ContextHandler:main: Started c.g.a.t.d.j.DevAppEngineWebAppContext@6dfcffb5{/,file:///Users/abc/dev/src/dtms/gcp/dtms-api-gcp/build/libs/exploded/dtms-api-gcp-0.0.1-SNAPSHOT.war/,AVAILABLE}{/Users/abc/dev/src/dtms/gcp/dtms-api-gcp/build/libs/exploded/dtms-api-gcp-0.0.1-SNAPSHOT.war}
...
c.g.a.t.development.DevAppServerImpl : Dev App Server is now running
答案 0 :(得分:2)
Spring Boot不支持使用Spring WebFlux进行WAR部署。