私は、メインクラスなしで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リクエストとして処理することができません。なぜ起こっているのかわかりません。
あなたはfound'.Iは、ビルド・パスが、エラーを設定しようとしたんコンパイラはあまりにも後に解決されていないばかりされていない」とまた私にエラーを与えているあなたのMavenのビルド・パス –
にサーバーAPIの依存関係が欠落している!何私はする必要がありますか? – Aayushi