Java 9-使用Spring 5和;拼图-可能吗?

Java 9-使用Spring 5和;拼图-可能吗?,java,spring,rest,maven,java-9,Java,Spring,Rest,Maven,Java 9,我一直在研究JDK9的拼图模块化系统 当从命令行编译时,它工作得非常好(至少在命令行中是这样) 但我正在尝试使用Spring和Jigsaw创建一个简单的RESTAPI。据说有 自2016年7月起与JDK 9完全兼容 我还希望RESTAPI与Maven一起工作。根据, Maven应该能够使用Java9(或者我读错了?) 当JAVA_主页指向JDK时,我似乎无法在我的spring boot应用程序上运行mvn spring boot:run——它会引发很多异常,例如: FoundError: jav

我一直在研究JDK9的拼图模块化系统

当从命令行编译时,它工作得非常好(至少在命令行中是这样)

但我正在尝试使用Spring和Jigsaw创建一个简单的RESTAPI。据说有

自2016年7月起与JDK 9完全兼容

我还希望RESTAPI与Maven一起工作。根据, Maven应该能够使用Java9(或者我读错了?)

当JAVA_主页指向JDK时,我似乎无法在我的spring boot应用程序上运行
mvn spring boot:run
——它会引发很多异常,例如:

FoundError: javax/xml/bind/ValidationException
2016-09-14 10:43:42.704 ERROR 120408 --- [           main] o.s.boot.SpringApplication               : Application startup failed
运行
mvn-version
可以提供:

Apache Maven 3.3.9 (bb52d8502b132ec0a5a3f4c09453c07478323dc5; 2015-11-10T17:41:47+01:00)
Maven home: C:\Program Files\apache-maven-3.3.9\bin\..
Java version: 9-ea, vendor: Oracle Corporation
Java home: C:\Program Files\Java\jdk-9
Default locale: en_US, platform encoding: Cp1252
OS name: "windows 10", version: "10.0", arch: "amd64", family: "dos"
目前是否可以使用Jigsaw使用Spring和Java9创建这个RESTAPI?或者我应该坚持使用Java 8直到2017年3月/7月左右

完全堆栈转储:

2016-09-14 10:43:42.688  WARN 120408 --- [           main] ationConfigEmbeddedWebApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.context.ApplicationContextException:                          Unable to start embedded container; nested exception is org.springframework.bea                         ns.factory.BeanCreationException: Error creating bean with name 'tomcatEmbeddedS                         ervletContainerFactory' defined in class path resource [org/springframework/boot                         /autoconfigure/web/EmbeddedServletContainerAutoConfiguration$EmbeddedTomcat.clas                         s]: Initialization of bean failed; nested exception is org.springframework.beans                         .factory.UnsatisfiedDependencyException: Error creating bean with name 'org.spri                         ngframework.boot.autoconfigure.web.HttpEncodingAutoConfiguration': Unsatisfied d                         ependency expressed through constructor parameter 0: Error creating bean with na                         me 'spring.http.encoding-org.springframework.boot.autoconfigure.web.HttpEncoding                         Properties': Initialization of bean failed; nested exception is java.lang.NoClas                         sDefFoundError: javax/xml/bind/ValidationException; nested exception is org.spri                         ngframework.beans.factory.BeanCreationException: Error creating bean with name '                         spring.http.encoding-org.springframework.boot.autoconfigure.web.HttpEncodingProp                         erties': Initialization of bean failed; nested exception is java.lang.NoClassDef                         FoundError: javax/xml/bind/ValidationException
2016-09-14 10:43:42.704 ERROR 120408 --- [           main] o.s.boot.SpringApplication               : Application startup failed

org.springframework.context.ApplicationContextException: Unable to start embedded container; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'tomcatEmbeddedServletContainerFactory'                          defined in class path resource [org/springframework/boot/autoconfigure/web/Embed                         dedServletContainerAutoConfiguration$EmbeddedTomcat.class]: Initialization of be                         an failed; nested exception is org.springframework.beans.factory.UnsatisfiedDepe                         ndencyException: Error creating bean with name 'org.springframework.boot.autocon                         figure.web.HttpEncodingAutoConfiguration': Unsatisfied dependency expressed thro                         ugh constructor parameter 0: Error creating bean with name 'spring.http.encoding                         -org.springframework.boot.autoconfigure.web.HttpEncodingProperties': Initializat                         ion of bean failed; nested exception is java.lang.NoClassDefFoundError: javax/xm                         l/bind/ValidationException; nested exception is org.springframework.beans.factor                         y.BeanCreationException: Error creating bean with name 'spring.http.encoding-org                         .springframework.boot.autoconfigure.web.HttpEncodingProperties': Initialization                          of bean failed; nested exception is java.lang.NoClassDefFoundError: javax/xml/bi                         nd/ValidationException
        at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:137) ~[spring-boot-1.4.0.RELEASE.jar:1.4.0.RELEASE]
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:535) ~[spring-context-4.3.2.RELEASE.jar:4.3.2.RELEASE]
        at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) ~[spring-boot-1.4.0.RELEASE.jar:1.4.0.RELEASE]
        at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:759) [spring-boot-1.4.0.RELEASE.jar:1.4.0.RELEASE]
        at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:369) [spring-boot-1.4.0.RELEASE.jar:1.4.0.RELEASE]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:313) [spring-boot-1.4.0.RELEASE.jar:1.4.0.RELEASE]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1185) [spring-boot-1.4.0.RELEASE.jar:1.4.0.RELEASE]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1174) [spring-boot-1.4.0.RELEASE.jar:1.4.0.RELEASE]
        at hello.Application.main(Application.java:10) [classes/:na]
        at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(java.base@9-ea/Native Method) ~[na:na]
        at jdk.internal.reflect.NativeMethodAccessorImpl.invoke(java.base@9-ea/NativeMethodAccessorImpl.java:62) ~[na:na]
        at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(java.base@9-ea/DelegatingMethodAccessorImpl.java:43) ~[na:na]
        at java.lang.reflect.Method.invoke(java.base@9-ea/Method.java:535) ~[na:na]
        at org.springframework.boot.maven.AbstractRunMojo$LaunchRunner.run(AbstractRunMojo.java:483) [spring-boot-maven-plugin-1.4.0.RELEASE.jar:1.4.0.RELEASE]
        at java.lang.Thread.run(java.base@9-ea/Thread.java:843) [na:na]
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'tomcatEmbeddedServletContainerFactory' defined in class path resource [org/springframework/boot/autoconfigure/web/EmbeddedServletContainerAut                         oConfiguration$EmbeddedTomcat.class]: Initialization of bean failed; nested exce                         ption is org.springframework.beans.factory.UnsatisfiedDependencyException: Error                          creating bean with name 'org.springframework.boot.autoconfigure.web.HttpEncodin                         gAutoConfiguration': Unsatisfied dependency expressed through constructor parame                         ter 0: Error creating bean with name 'spring.http.encoding-org.springframework.b                         oot.autoconfigure.web.HttpEncodingProperties': Initialization of bean failed; ne                         sted exception is java.lang.NoClassDefFoundError: javax/xml/bind/ValidationExcep                         tion; nested exception is org.springframework.beans.factory.BeanCreationExceptio                         n: Error creating bean with name 'spring.http.encoding-org.springframework.boot.                         autoconfigure.web.HttpEncodingProperties': Initialization of bean failed; nested                          exception is java.lang.NoClassDefFoundError: javax/xml/bind/ValidationException
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
        at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
        at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.getEmbeddedServletContainerFactory(EmbeddedWebApplicationContext.java:199) ~[spring-boot-1.4.0.RELEASE.jar:1.4.0.RELEASE]
        at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.createEmbeddedServletContainer(EmbeddedWebApplicationContext.java:162) ~[spring-boot-1.4.0.RELEASE.jar:1.4.0.RELEASE]
        at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:134) ~[spring-boot-1.4.0.RELEASE.jar:1.4.0.RELEASE]
        ... 14 common frames omitted
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.boot.autoconfigure.web.HttpEncodingAutoConfiguration': Unsatisfied dependency expressed through constructor para                         meter 0: Error creating bean with name 'spring.http.encoding-org.springframework                         .boot.autoconfigure.web.HttpEncodingProperties': Initialization of bean failed;                          nested exception is java.lang.NoClassDefFoundError: javax/xml/bind/ValidationExc                         eption; nested exception is org.springframework.beans.factory.BeanCreationExcept                         ion: Error creating bean with name 'spring.http.encoding-org.springframework.boo                         t.autoconfigure.web.HttpEncodingProperties': Initialization of bean failed; nest                         ed exception is java.lang.NoClassDefFoundError: javax/xml/bind/ValidationExcepti                         on
        at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:749) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
        at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:189) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1143) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1046) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:510) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
        at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
        at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:372) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1123) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1018) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:510) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
        at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE] 
----切断

摘要错误:

[ERROR] Failed to execute goal org.springframework.boot:spring-boot-maven-plugin                         
:1.4.0.RELEASE:run (default-cli) on project gs-rest-service: An exception occurr                         
ed while running. null: InvocationTargetException: Unable to start embedded cont                         
ainer; nested exception is org.springframework.beans.factory.BeanCreationExcepti       
on: Error creating bean with name 'tomcatEmbeddedServletContainerFactory' define                         
d in class path resource [org/springframework/boot/autoconfigure/web/EmbeddedSer
vletContainerAutoConfiguration$EmbeddedTomcat.class]: Initialization of bean fai                    
led; nested exception is org.springframework.beans.factory.UnsatisfiedDependency              
Exception: Error creating bean with name 'org.springframework.boot.autoconfigure    
.web.HttpEncodingAutoConfiguration': Unsatisfied dependency expressed through co                         
nstructor parameter 0: Error creating bean with name 'spring.http.encoding-org.s           
pringframework.boot.autoconfigure.web.HttpEncodingProperties': Initialization of                          
bean failed; nested exception is java.lang.NoClassDefFoundError: javax/xml/bind                         
/ValidationException; nested exception is org.springframework.beans.factory.Bean                      
CreationException: Error creating bean with name 'spring.http.encoding-org.sprin            
gframework.boot.autoconfigure.web.HttpEncodingProperties': Initialization of bea                         
n failed; nested exception is java.lang.NoClassDefFoundError: javax/xml/bind/Val                         
idationException: javax.xml.bind.ValidationException -> [Help 1]

具体来说,您缺少一个依赖项。

因为您使用的是非默认模块,所以在编译和运行Java9时需要手动添加它:

javac --add-modules java.xml.bind ...
java  --add-modules java.xml.bind ...
如果缺少其他java ee类,请尝试添加桶模块:

--add-module java.se.ee

罗夏的答案是不正确的,因为hibernate验证异常在不同的包、不同的类中。我没有足够的代表点直接发表评论

zhy2002的答案对于仅Java-9的情况是正确的,但对于Java 8或10不起作用(因为此Java ee模块已弃用,将在10中删除)。如果您希望它像我们一样使用Java 8和Java 9,可以添加以下依赖项:

格拉德尔:

compile "javax.xml.bind:jaxb-api:2.3.0"
马文:

<!-- https://mvnrepository.com/artifact/javax.xml.bind/jaxb-api -->
<dependency>
    <groupId>javax.xml.bind</groupId>
    <artifactId>jaxb-api</artifactId>
    <version>2.3.0</version>
</dependency>

javax.xml.bind

如果是弹簧靴,则需要在maven pom中添加:

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <configuration>
         <jvmArguments>--add-modules java.xml.bind</jvmArguments>
         <mainClass>org.springframework.samples.petclinic.PetClinicApplication</mainClass>
     </configuration>
...

org.springframework.boot

添加整个堆栈,我们实际上想看看您遇到了什么异常。“应用程序启动失败”对我们毫无用处。只需剪掉最低级的异常。这隐式地是由于使用Java9作为JDK造成的,因为这个RESTAPI使用SpringBoot,并且还没有SpringBoot版本与JDK9/模块兼容?还是有?你添加依赖项了吗?怎么搞的?
<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <configuration>
         <jvmArguments>--add-modules java.xml.bind</jvmArguments>
         <mainClass>org.springframework.samples.petclinic.PetClinicApplication</mainClass>
     </configuration>
...
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
    <exclusions>
         <exclusion>
             <artifactId>javassist</artifactId>
             <groupId>org.javassist</groupId>
         </exclusion>
    </exclusions>
</dependency>
<dependency>
    <groupId>org.javassist</groupId>
    <artifactId>javassist</artifactId>
    <version>3.22.0-GA</version>
    <scope>runtime</scope>
</dependency>