2015-09-09 46 views
5

どのようにしてPOSTメソッドがSpringブートMVCでサポートされないのですか?Spring Boot 405 POSTメソッドはサポートされていませんか?

http://localhost:8090/backoffice/tags/add 

リクエストボディ::

[{"tagName":"qweqwe"},{"tagName":"zxczxczx"}] 

ここで、このように、このURLを叩く私のコード

@RestController(value="/backoffice/tags") 
public class TagsController { 

    @RequestMapping(value = "/add", method = RequestMethod.POST) 
     public void add(@RequestBody List<Tag> keywords) { 
      tagsService.add(keywords); 
     } 
} 

です:私は、エンティティのリストを受け入れ、単純なpostメソッドを実装しようとしています私は受け取った:

{ 
    "timestamp": 1441800482010, 
    "status": 405, 
    "error": "Method Not Allowed", 
    "exception": "org.springframework.web.HttpRequestMethodNotSupportedException", 
    "message": "Request method 'POST' not supported", 
    "path": "/backoffice/tags/add" 
} 

EDIT

デバッグ春のWebリクエストハンドラ

 public void handleRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
      this.checkRequest(request); 

protected final void checkRequest(HttpServletRequest request) throws ServletException { 
     String method = request.getMethod(); 
     if(this.supportedMethods != null && !this.supportedMethods.contains(method)) { 
      throw new HttpRequestMethodNotSupportedException(method, StringUtils.toStringArray(this.supportedMethods)); 
     } else if(this.requireSession && request.getSession(false) == null) { 
      throw new HttpSessionRequiredException("Pre-existing session required but none found"); 
     } 
    } 

supportedMethodsで唯一の二つの方法は、あなたがRestController注釈の定義に誤りがあり{GET,HEAD}

+0

更新もTagsControllerのコード。 –

+3

リビジョン2にロールバックすることを検討してください。今、あなたの質問のコードは正しいので、答えは無意味です。 @RafalG。 – approxiblue

+0

。問題のコードは「固定」されているとはみなされません。 – eis

答えて

9

です。ドキュメントによれば、次のとおりです

RestControllerは{

/** *値が*の場合のばねBeanに点灯する、論理コンポーネント 名の提案を示すことができる公共@interface自動検出 コンポーネント。 * @since 4.0.1 */String value()default "";あなたが(「/バックオフィス/タグ」)入力した値を意味

}

それが利用可能であるその下のコントローラではないパス名です。

コントローラーのクラスに@RequestMapping("/backoffice/tags")を追加し、@RestControllerアノテーションの値を削除します。

編集: このコードを使用してみてください。そして、IDEからローカルで実行してください。

build.gradle

buildscript { 
    ext { 
     springBootVersion = '1.2.5.RELEASE' 
    } 
    repositories { 
     mavenCentral() 
    } 
    dependencies { 
     classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") 
     classpath("io.spring.gradle:dependency-management-plugin:0.5.2.RELEASE") 
    } 
} 

apply plugin: 'java' 
apply plugin: 'eclipse' 
apply plugin: 'idea' 
apply plugin: 'spring-boot' 
apply plugin: 'io.spring.dependency-management' 

jar { 
    baseName = 'demo' 
    version = '0.0.1-SNAPSHOT' 
} 
sourceCompatibility = 1.8 
targetCompatibility = 1.8 

repositories { 
    mavenCentral() 
} 


dependencies { 
    compile("org.springframework.boot:spring-boot-starter-web") 
    testCompile("org.springframework.boot:spring-boot-starter-test") 
} 


eclipse { 
    classpath { 
     containers.remove('org.eclipse.jdt.launching.JRE_CONTAINER') 
     containers 'org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8' 
    } 
} 

task wrapper(type: Wrapper) { 
    gradleVersion = '2.3' 
} 

Tag.java

package demo; 

import com.fasterxml.jackson.annotation.JsonCreator; 
import com.fasterxml.jackson.annotation.JsonProperty; 

public class Tag { 

    private final String tagName; 

    @JsonCreator 
    public Tag(@JsonProperty("tagName") String tagName) { 
     this.tagName = tagName; 
    } 

    public String getTagName() { 
     return tagName; 
    } 

    @Override 
    public String toString() { 
     final StringBuilder sb = new StringBuilder("Tag{"); 
     sb.append("tagName='").append(tagName).append('\''); 
     sb.append('}'); 
     return sb.toString(); 
    } 
} 

SampleController.java

package demo; 

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

import java.util.List; 

@RestController 
@RequestMapping("/backoffice/tags") 
public class SampleController { 

    @RequestMapping(value = "/add", method = RequestMethod.POST) 
    public void add(@RequestBody List<Tag> tags) { 
     System.out.println(tags); 
    } 
} 

DemoApplication.java

package demo; 

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

@SpringBootApplication 
public class DemoApplication { 

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

さて、それは1つですが、それでもまだ動作していません...それを変更しても – Adelin

+0

私にそれを再現させてください。ギムメ5分。 –

+0

@Adelinください、完全な作業サンプルをご覧ください。 –

関連する問題