0

私はWebデベロップメントにはまったく新しいので、今年の夏にSpringのWebアプリケーションで作業しましたが、アプリはすでに設定されていて、JavaとAngularJSをやっていました。しかし、今私は最初からいくつかの問題があるこのタイプのアプリケーションを開始する。私は単純なWebサービスをテストしようとしていましたが、私が望むURLにアクセスできない場合、404エラーが発生します。私は、Tomcat 8.5.4でのIntelliJに取り組んでいます:シンプルな休憩サービスで404エラーが発生しました

のpom.xml

<?xml version="1.0" encoding="UTF-8"?> 
    <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>seb</groupId> 
    <artifactId>webapp</artifactId> 
    <packaging>war</packaging> 
    <version>1.0-SNAPSHOT</version> 

    <build> 
     <plugins> 
      <plugin> 
       <groupId>org.apache.maven.plugins</groupId> 
       <artifactId>maven-compiler-plugin</artifactId> 
       <version>3.5.1</version> 
       <configuration> 
        <source>1.8</source> 
        <target>1.8</target> 
       </configuration> 
      </plugin> 
     </plugins> 
    </build> 

    <dependencies> 
     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot</artifactId> 
      <version>1.4.0.RELEASE</version> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework.data</groupId> 
      <artifactId>spring-data-jpa</artifactId> 
      <version>1.10.2.RELEASE</version> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-web</artifactId> 
      <version>4.3.2.RELEASE</version> 
     </dependency> 
     <dependency> 
      <groupId>javax.servlet</groupId> 
      <artifactId>javax.servlet-api</artifactId> 
      <version>3.1.0</version> 
     </dependency> 
     <dependency> 
      <groupId>javax.persistence</groupId> 
      <artifactId>persistence-api</artifactId> 
      <version>1.0.2</version> 
      <scope>provided</scope> 
     </dependency> 
    </dependencies> 

</project> 

アプリケーションのcontext.xml

<?xml version="1.0" encoding="UTF-8"?> 
<beans:beans xmlns:beans="http://www.springframework.org/schema/beans" 
      xmlns="http://www.springframework.org/schema/beans" 
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
      xmlns:mvc="http://www.springframework.org/schema/mvc" 
      xmlns:jpa="http://www.springframework.org/schema/data/jpa" 
      xmlns:context="http://www.springframework.org/schema/context" 
      xsi:schemaLocation="http://www.springframework.org/schema/beans 
     http://www.springframework.org/schema/beans/spring-beans.xsd 
     http://www.springframework.org/schema/data/jpa 
     http://www.springframework.org/schema/data/jpa/spring-jpa.xsd 
     http://www.springframework.org/schema/context"> 

     <jpa:repositories base-package="repository" /> 
     <mvc:annotation-driven/> 
     <context:component-scan base-package="controller" /> 

</beans:beans> 

web.xmlの

<!DOCTYPE web-app PUBLIC 
     "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" 
     "http://java.sun.com/dtd/web-app_2_3.dtd" > 

<web-app> 
    <display-name>Archetype Created Web Application</display-name> 
    <servlet> 
     <servlet-name>springrest</servlet-name> 
     <servlet-class> 
      org.springframework.web.servlet.DispatcherServlet 
     </servlet-class> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 

    <servlet-mapping> 
     <servlet-name>springrest</servlet-name> 
     <url-pattern>/</url-pattern> 
    </servlet-mapping> 
</web-app> 

メイン。 Java

@SpringBootConfiguration 
@EnableJpaRepositories("repository") 
public class Main { 

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

StudentController.java

@RestController 
public class StudentController { 

    private final StudentService studentService; 

    public StudentController(final StudentService studentService) { 
     this.studentService = studentService; 
    } 

    @RequestMapping(value = "/student", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE) 
    public void getSomeStudent() { 
     studentService.getSomeStudent(); 
     System.out.println("Hello"); 
    } 
} 

は私が到達しようとしているURLは次のとおりです。ちょうど404エラーを持っている、8080/Webアプリケーション/学生が、何も起こりません:localhostを。私は何かを忘れてしまったと思うが、Web上のチュートリアルや質問を見て、私のプロジェクトを修正する方法を理解することはできない。だから誰かが私を助ける手がかりを得たら、それは非常に役に立つでしょう。事前に感謝します。

+0

Spring Bootを使用しますか?あなたは '@ SpringBootConfiguration'を持っていますが、無効な設定ファイルを持っているからです。それ以外の場合は、プロジェクトが正常に起動した場合は、webappなしで 'localhost:8080/student'を取得してみてください。 – jahra

+0

'@SpringBootConfiguration'の代わりに '@SpringBootApplication'を追加してください。あなたが春のブートを使用している場合 – swan

+0

春のブートプロジェクトを作成したい場合は、春のドキュメントguidに従ってください:https://spring.io/guides/gs/spring-boot/ – swan

答えて

-1

https://start.spring.io/を使用してSpringブートアプリケーションプロジェクトを作成します。

これは、あなたのための春のブートMavenプロジェクトを生成します。

そこでStudentControllerを追加します。 @jahra指定された、あなたはURLをトリガーする必要があるとして

とそののafer:http://localhost:8080/studentそれを@SpringBootApplicationするためにあなたの学生のWebサービス

0

変更@SpringBootConfigurationがあなたのために

  • @Configurationタグを次のように構成アクセスしますアプリケーションコンテキストのBean定義のソースとしてのクラス
  • @EnableAutoConfigurationは、クラスパス設定、他のBean、およびさまざまなプロパティー の設定に基づいてBeanの追加をSpringブートに指示します。通常、@EnableWebMvcをSpring MVCアプリケーションの に追加しますが、spring-webmvcがクラスパスの にあると自動的に追加されます。これにより、アプリケーションにWebアプリケーションとしてフラグが設定され、 は、設定などの主要な動作を有効にします。
  • @ComponentScanはhelloパッケージ内の他のコンポーネント、設定、サービスを探すようSpringに指示します。 HelloControllerを見つけることができます。

spring-bootを使用しているので、次の依存関係を追加してWebを削除してください。春ブートが自動的

<dependency> 
 
     <groupId>org.springframework.boot</groupId> 
 
     <artifactId>spring-boot-starter-web</artifactId> 
 
    </dependency>

またはあなたはまだ先に行くと、あなたがしたい場合は、1つを設定することができますするためのWebディスパッチャサーブレットを設定し、XML。あなたの初期設定に基づいて

は、コントローラは

http://localhost:8080/student(no「をWebアプリケーション」)に位置しなければならない

0

さて私は、私はちょうど今、私のバネを使用していTomcatで私のアプリを展開しようと停止私のURLに到達することができます。とにかく、私は春のドキュメントやチュートリアルをもっと深く見ていきます。さまざまな説明とリンクのおかげです。

関連する問題