2016-10-30 10 views
2

this tutorialの後にSpringブートを使用して簡単なRESTサービスを作成しようとしています。 webappフォルダ(index.html)からの "Hello World" htmlファイルがhttp://localhost:8080/my-rest-app/にオープンしています(私はMaven-Web-Appを作成しました。なぜなら、サービスの "ようこそページ"を持っているからです)。しかし、http://localhost:8080/my-rest-app/userでRESTサービスにアクセスしようとすると、404メッセージが表示されます。Springブート自動設定でRESTコントローラにアクセスできない

の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>service</groupId> 
    <artifactId>my-rest-app</artifactId> 
    <version>1.0-SNAPSHOT</version> 
    <packaging>war</packaging> 

    <name>my-rest-app</name> 

    <properties> 
     <endorsed.dir>${project.build.directory}/endorsed</endorsed.dir> 
     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 
    </properties> 

    <parent> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-parent</artifactId> 
     <version>1.4.1.RELEASE</version> 
     <relativePath/> <!-- lookup parent from repository --> 
    </parent> 

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

    <build> 
     <plugins> 
      <plugin> 
       <groupId>org.springframework.boot</groupId> 
       <artifactId>spring-boot-maven-plugin</artifactId> 
      </plugin> 
     </plugins> 
    </build> 

</project> 

Application.java:

package service.my.rest.app; 

import org.springframework.boot.SpringApplication; 
import org.springframework.boot.autoconfigure.SpringBootApplication; 

@SpringBootApplication 
//@ComponentScan({"service.my.rest.app", "service.my.rest.app.controller"}) 
//@ComponentScan(basePackageClasses = UserController.class) 
public class Application { 

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

} 

UserController.java:

package service.my.rest.app.controller; 

import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 
import org.springframework.web.bind.annotation.RestController; 

@RestController 
@RequestMapping("/user") 
public class UserController { 

    @RequestMapping(method = RequestMethod.GET) 
    public String getUser() { 
     return "Hello"; 
    } 

} 

私は何をしないのですか?サービスへのURLが間違っていますか?私は、SpringブートRESTサービスのコンテキストパスが常に/であることを読むと、http://localhost:8080/my-rest-app/以上のサービスにアクセスする必要があることを意味しますが、これはエーテルが動作しない(index.htmlなし)。 application.propertiesのコンテキストパスをserver.contextPath=/my-rest-appserver.port=8080に変更しても、役に立たなかった。

+1

瓶だけでなく戦争用のパッケージを使用している特定の理由はありますか? – chrylis

+0

'getUser()'メソッドから '@RequestMapping(method = RequestMethod.GET)'を削除します。注釈なしでgetメソッドを使って呼び出す+あなたは値を指定していない。 –

+0

@chrylis通常のWebページと共にTomcat上でRESTサービスを実行する必要があります。したがって、htmlファイルとサービスは同じwarパッケージに含まれています。実際に私は選択しなかった。 Netbeansはパッケージングとサーバー上での展開を行いました。 – deve

答えて

1

問題はビルドとデプロイのプロセスでした。ヒントのための@chrylisありがとうございました。

package service.my.rest.app; 

import org.springframework.boot.SpringApplication; 
import org.springframework.boot.autoconfigure.SpringBootApplication; 
import org.springframework.boot.builder.SpringApplicationBuilder; 
import org.springframework.boot.web.support.SpringBootServletInitializer; 
import org.springframework.context.annotation.ComponentScan; 

@SpringBootApplication 
@ComponentScan({"service.my.rest.app"}) 
public class Application extends SpringBootServletInitializer { 

    private static Class<Application> application = Application.class; 

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

    @Override 
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) { 
     return builder.sources(application); 
    } 

} 

私はSpring Boot is actually designed to produce a jarので、build and deploy a war fileに私のアプリを拡張する必要がありました。

関連する問題