2017-06-10 9 views
1

REST API開発用のJava Spring Bootの学習を始めました。以下のコードでは、GETメソッドは正常に動作していますが、POSTは機能しません。 Content-TypeSpringbootのリクエストメソッド 'POST'はサポートされていません

application/jsonとしてエラーを使用したポストマンアプリでPOSTメソッド、私が見ることができるログに

{ 
    "timestamp": 1497116929266, 
    "status": 405, 
    "error": "Method Not Allowed", 
    "exception": "org.springframework.web.HttpRequestMethodNotSupportedException", 
    "message": "Request method 'POST' not supported", 
    "path": "/api/users/" 
} 

s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/api/users],methods=[POST]}" onto public java.lang.String com.betasquirrel.controller.UsersController.saveUser() 
s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/api/users],methods=[GET]}" onto public java.util.List<com.betasquirrel.model.User> com.betasquirrel.controller.UsersController.getAll() 
s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest) 
s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse) 
o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 
o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 
o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 

MavenのためのJavaとspringboot版テスト済み

@RestController 
@RequestMapping("/api/users") 
public class UsersController { 

    @Autowired 
    private UserRepository userRepository; 

    @RequestMapping(method = RequestMethod.GET) 
    public List<User> getAll() { 
     return userRepository.findAll(); 
    } 

    @RequestMapping(method = RequestMethod.POST) 
    public String saveUser() { 
     return "Saved"; 
    } 

} 

<parent> 
    <groupId>org.springframework.boot</groupId> 
    <artifactId>spring-boot-starter-parent</artifactId> 
    <version>1.5.4.RELEASE</version> 
    <relativePath/> 
</parent> 

<properties> 
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> 
    <java.version>1.8</java.version> 
</properties> 
+0

あなたのエラーは、フィールド 'path'と値'/api/users/'を持つjsonを表示します。多分パスの問題の終わりのスラッシュ? – berserkk

+0

@berserkkは 'api/users'にアクセスしようとしました。' 404'、 'api/users /'は '405'と表示します。 – devo

+0

あなたが切ったログメッセージ全体を投稿します。 – chrylis

答えて

0

最後に、mavenコマンドを使用してプロジェクトを実行して問題を解決しました。代わりに、IDEの実行]ボタンを使用してプロジェクトを実行しているの

は、単に、IDE内の端末に行って、それが今、私はこれらにアクセスすることができ、REST実装であるため、コマンド

mvn spring-boot:run 

を実行

GET /api/users/ or /api/users 
POST /api/users/ or /api/users 

魅力のように働く!

+0

古いコードを実行していたようです。 – chrylis

+0

@chrylisいいえ、私はそれがIDEの実行(IntelliJのアイデア)の問題だと確信しています。多くの場合、実行ボタンでコードを実行しましたが、機能しませんでした。 mavenコマンドを使用して実行した後、完全に動作しました。 – devo

+0

Mavenを使用して実行した後、IDEで作業を開始しましたか? – chrylis

関連する問題