2015-09-09 286 views
5

怎麼可能POST方法不被Spring Boot MVC支持?我想實現一個接受實體列表一個簡單的POST方法:這裏是我的代碼Spring Boot 405不支持POST方法?

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

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

達不到這個URL是這樣的:

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

請求正文:

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

我收到:

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

編輯

調試的Spring 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只有兩種方法{GET,HEAD}

+0

更新TagsController的代碼。 –

+3

考慮回滾到修訂2。現在,你的問題中的代碼是正確的,使答案毫無意義。 – approxiblue

+0

@RafalG。有關的代碼不應該是「固定的」,它打破了這個問題。 – eis

回答

9

你有RestController註釋定義錯誤。根據該文檔,它是:

公共@interface RestController {

/** *可以指示用於一個邏輯組件 名稱的建議值,*要變成一個Spring bean中的情況下自動檢測 組件。 * @返回建議的組件名稱,如果有的話* @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

好吧,這是一個,但它仍然無法正常工作...即使改變它 – Adelin

+0

讓我重現它。給我5分鐘。 –

+0

@Adelin請參閱完整的工作示例。 –

相關問題