2017-04-15 118 views
2

我試圖實現一個沒有主類的Spring引導應用程序。這裏是我迄今爲止所做的。Spring引導NoClassDefFoundException

import org.springframework.boot.builder.SpringApplicationBuilder; 
    import org.springframework.boot.web.support.SpringBootServletInitializer; 
    import org.springframework.boot.autoconfigure.SpringBootApplication; 

@SpringBootApplication 
    public class Application extends SpringBootServletInitializer{ 

      @Override 
      public SpringApplicationBuilder configure(SpringApplicationBuilder builder){ 
       return builder.sources(Application.class); 
      } 
    } 

我的控制器類看起來像這樣

import java.io.*; 

import javax.servlet.http.HttpServletRequest; 

import org.json.simple.JSONObject; 
import org.json.simple.parser.JSONParser; 
import org.json.simple.parser.ParseException; 
import org.springframework.http.MediaType; 
import org.springframework.web.bind.annotation.RequestBody; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RestController; 

@RestController 
public class Controller { 

    @RequestMapping(path="/api",consumes=MediaType.APPLICATION_JSON_VALUE) 
    public String insertData(@RequestBody String rawJsonData,HttpServletRequest request) throws ParseException, IOException{ 
     JSONParser jparser=new JSONParser(); 
     Object jsonObj=jparser.parse(rawJsonData); 
     JSONObject jObject=(JSONObject)jsonObj; 

     FileWriter filewriter=new FileWriter(new File("C:\\Users\\Desktop\\Aayushi.txt")); 
     filewriter.write(jObject.toJSONString()); 
     return rawJsonData; 

    } 
} 

而且pom.xml中具有以下依存關係

<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>API</groupId> 
    <artifactId>API</artifactId> 
    <version>0.0.1-SNAPSHOT</version> 
    <packaging>war</packaging> 
    <build> 
    <sourceDirectory>src</sourceDirectory> 
    <plugins> 
     <plugin> 
     <artifactId>maven-compiler-plugin</artifactId> 
     <version>3.5.1</version> 
     <configuration> 
      <source>1.8</source> 
      <target>1.8</target> 
     </configuration> 
     </plugin> 
     <plugin> 
     <artifactId>maven-war-plugin</artifactId> 
     <version>3.0.0</version> 
     <configuration> 
      <warSourceDirectory>WebContent</warSourceDirectory> 
     </configuration> 
     </plugin> 
     <plugin> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-maven-plugin</artifactId> 
      <configuration> 
      <mainClass>${start-class}</mainClass> 
     </configuration> 
     </plugin> 

    </plugins> 
    </build> 
    <parent> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-parent</artifactId> 
     <version>1.4.2.RELEASE</version> 
    </parent> 
    <dependencies> 
     <dependency> 
      <groupId>com.googlecode.json-simple</groupId> 
      <artifactId>json-simple</artifactId> 
      <version>1.1</version> 
     </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><!-- Add tomcat only if I want to run directly --> 

    </dependencies> 
    <properties> 
     <start-class>Application</start-class> 
     <java.version>1.8</java.version> 
    </properties> 
</project> 

但是,當我使用MVN春天啓動運行上面的代碼:運行那麼它給了我以下錯誤。

[INFO] Scanning for projects... 
[INFO]                   
[INFO] ------------------------------------------------------------------------ 
[INFO] Building API 0.0.1-SNAPSHOT 
[INFO] ------------------------------------------------------------------------ 
[INFO] 
[INFO] >>> spring-boot-maven-plugin:1.4.2.RELEASE:run (default-cli) > test-compile @ API >>> 
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ API --- 
[INFO] Using 'UTF-8' encoding to copy filtered resources. 
[INFO] skip non existing resourceDirectory C:\Users\aayus\workspace_neon\API\src\main\resources 
[INFO] skip non existing resourceDirectory C:\Users\aayus\workspace_neon\API\src\main\resources 
[INFO] 
[INFO] --- maven-compiler-plugin:3.5.1:compile (default-compile) @ API --- 
[INFO] Nothing to compile - all classes are up to date 
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ API --- 
[INFO] Using 'UTF-8' encoding to copy filtered resources. 
[INFO] skip non existing resourceDirectory C:\Users\aayus\workspace_neon\API\src\test\resources 
[INFO] 
[INFO] --- maven-compiler-plugin:3.5.1:testCompile (default-testCompile) @ API --- 
[INFO] No sources to compile 
[INFO] 
[INFO] <<< spring-boot-maven-plugin:1.4.2.RELEASE:run (default-cli) < test-compile @ API <<< 
[INFO] 
[INFO] --- spring-boot-maven-plugin:1.4.2.RELEASE:run (default-cli) @ API --- 
[WARNING] 
java.lang.NoClassDefFoundError: javax/servlet/ServletContext 
    at java.lang.Class.getDeclaredMethods0(Native Method) 
    at java.lang.Class.privateGetDeclaredMethods(Unknown Source) 
    at java.lang.Class.privateGetMethodRecursive(Unknown Source) 
    at java.lang.Class.getMethod0(Unknown Source) 
    at java.lang.Class.privateGetMethodRecursive(Unknown Source) 
    at java.lang.Class.getMethod0(Unknown Source) 
    at java.lang.Class.getMethod(Unknown Source) 
    at org.springframework.boot.maven.AbstractRunMojo$LaunchRunner.run(AbstractRunMojo.java:502) 
    at java.lang.Thread.run(Unknown Source) 
Caused by: java.lang.ClassNotFoundException: javax.servlet.ServletContext 
    at java.net.URLClassLoader.findClass(Unknown Source) 
    at java.lang.ClassLoader.loadClass(Unknown Source) 
    at java.lang.ClassLoader.loadClass(Unknown Source) 
    ... 9 more 
[INFO] ------------------------------------------------------------------------ 
[INFO] BUILD FAILURE 
[INFO] ------------------------------------------------------------------------ 
[INFO] Total time: 3.456 s 
[INFO] Finished at: 2017-04-16T02:50:22+05:30 
[INFO] Final Memory: 19M/177M 
[INFO] ------------------------------------------------------------------------ 
[ERROR] Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:1.4.2.RELEASE:run (default-cli) on project API: An exception occurred while running. javax/servlet/ServletContext: javax.servlet.ServletContext -> [Help 1] 
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch. 
[ERROR] Re-run Maven using the -X switch to enable full debug logging. 
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles: 
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException 

我不明白我在這做錯了什麼。另外,我的代碼無法將HttpServletRequest作爲Http請求處理。不知道爲什麼會發生這種情況。

+0

你缺少對服務器API的依賴在你的Maven構建路徑 –

+0

它也給了我一個錯誤「沒有編譯器found'.I嘗試配置構建路徑,但錯誤沒有得到後也解決了!什麼我應該怎麼做? – Aayushi

回答

2

你可以嘗試下面添加。請讓我們知道它是否有幫助!

<!-- https://mvnrepository.com/artifact/javax.servlet/servlet-api --> 
<dependency> 
    <groupId>javax.servlet</groupId> 
    <artifactId>servlet-api</artifactId> 
    <version>3.0.1</version> 
    <scope>provided</scope> 
</dependency> 

同時添加main()方法到您的應用程序類...沒有它,我認爲你的春天的應用程序將無法運行。

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

小東西。 Web服務器應用程序運行時可能會捆綁servlet api。最好加上'提供的'。 – Nomad

+0

即使在添加依賴關係之後,您仍然正確 – ProgrammerBoy

+0

,我得到這些錯誤 [警告] java.lang.Exception:指定的mainClass不包含具有適當簽名的main方法。 \t在org.springframework.boot.maven.AbstractRunMojo $ LaunchRunner.run(AbstractRunMojo.java:509) \t在java.lang.Thread.run(Thread.java:745) 造成的:java.lang.NoSuchMethodException: (java.lang.Class.getMethod(Class.java:1786) \t at org.springframework.boot.maven.AbstractRunMojo $ LaunchRunner.run(AbstractRunMojo.java: 502) \t ... 1更多和構建失敗@SanketD – Aayushi