2015-09-09 17 views
5

In che modo il metodo POST non può essere supportato da Spring Boot MVC?! Sto cercando di implementare un metodo semplice post che accetta un elenco delle entità: Ecco il mio codiceSpring Boot 405 Il metodo POST non è supportato?

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

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

Colpire questo URL come questo:

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

Richiesta Corpo:

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

Ricevo:

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

EDIT:

Debugging Primavera Web gestore richieste

 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"); 
     } 
    } 

Gli unici due metodi in supportedMethods sono {GET,HEAD}

+0

aggiornare il Codice TagsController pure. –

+3

Considera di tornare alla revisione 2.In questo momento, il codice nella tua domanda è corretto, rendendo la risposta priva di significato. – approxiblue

+0

@RafalG. il codice in questione non dovrebbe essere "riparato", rompe la domanda. – eis

risposta

9

Hai un errore nella definizione RestController annotazione. Secondo la documentazione che è:

@interface pubblico RestController {

/** * Il valore può indicare un suggerimento per una logica nome del componente , * per essere trasformato in un bean Spring in caso di un componente rilevato automaticamente. * @return il nome del componente suggerito, se presente * @since 4.0.1 */String value() default "";

}

Il che significa che il valore immesso ("/ backoffice/tag") è il nome del comando, non il percorso in cui è disponibile.

Aggiungere @RequestMapping("/backoffice/tags") sulla classe del controller e rimuovere il valore dall'annotazione @RestController.

EDIT: esempio completamente funzionante come da commento che non funziona - tenta di utilizzare il codice di registrazione - e gestito localmente da 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

Ok, questo è uno ma non funziona ancora ... anche dopo averlo modificato. – Adelin

+0

Lasciami riprodurre. Dammi 5 minuti. –

+0

@Adelin per favore, vedere il campione completo di lavoro. –

Problemi correlati