2017-06-15 2 views
1

Wie kann ich mehrere Beans in meinem @RestController zuordnen?Wie mehrere Beans in Spring @RestController zuordnen?

Ich bin mit Feder-web-4.3.8.RELEASE.jar

Ich habe alles versucht: @RequestParam @RequestBody, @RequestAttribute, @RequestPart aber nichts funktioniert ...

package com.example.demo; 

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

@RestController 
public class RestService { 

    @RequestMapping(value = "demo", method = RequestMethod.POST, consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE) 
    public Object[] demo(Foo foo, Bar bar) { 
     return new Object[]{foo, bar}; 
    } 

    public static class Bar { 
     public Long id; 
     public String bar; 
    } 

    public static class Foo { 
     public Long id; 
     public String foo; 
    } 
} 

My (codiert) Nutzlast:

foo=%7B%22id%22%3A123%2C%22foo%22%3A%22foo1%22%7D&bar=%7B%22id%22%3A456%2C%22bar%22%3A%22bar1%22%7D

Decoded Nutzlast:

foo={"id":123,"foo":"foo1"}&bar={"id":456,"bar":"bar1"}

Anforderungsheader:

Content-Type: application/x-www-form-urlencoded

Mit dem obigen Code, gibt sie:

[{"id":null,"foo":null},{"id":null,"bar":null}]

Aber was ich will, ist:

[{"id":123,"foo":"foo1"},{"id":456,"bar":"bar1"}]

Dank

+0

möglich duplicate von: https://stackoverflow.com/questions/20622359/automatic-conversion-of-json-form-parameter-in-spring-mvc-4-0 – chuckskull

+0

@Freddy Boucher überprüfen Sie bitte meine bearbeiten. –

Antwort

0

Sie sind Erstellen von statischen inneren cla ss in Ihrem RestController. Spring wird nicht in der Lage sein, die Eigenschaften aus der empfangenen Anfrage automatisch mit der erwähnten Bean abzubilden. Bitte definieren Sie Ihre Bean in einem separaten Paket oder außerhalb des Controllers. Dann können Sie es mit @RequestBody abbilden.

  @RestController 
      public class RestService { 

       @RequestMapping(value = "demo", method = RequestMethod.POST, consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE) 
       public Object[] demo(@RequestBody FooBar foobar) { 
         // your custom work 
       } 
      } 


       public class Bar { 
        public Long id; 
        public String bar; 
       } 

       public class Foo { 
        public Long id; 
        public String foo; 
       } 

// created wrapper as @RequestBody can be used only with one argument. 
       public class FooBar { 
         private Foo foo; 
         private Bar bar; 
       } 

für referece bitte überprüfen requestBody with multiple beans

auch dafür sorgen, dass die Anfrage params Namen mit den Eigenschaften der Bohne passen. (D Foo und Bar).

+0

Hallo @Sangam Belose. Bohnen als innere Klasse definiert ist nicht das Problem, aber trotzdem habe ich Ihre Lösung getestet, aber es funktioniert nicht. Ich erhalte den folgenden Fehler: {"Zeitstempel": 1497515020279, "Status": 415, "Fehler": "Nicht unterstützter Medientyp", "Ausnahme": "org.springframework.web.HttpMediaTypeNotSupportedException", "Nachricht": " Inhaltstyp 'application/x-www-form-urlencoded; Zeichensatz = UTF-8' wird nicht unterstützt "," path ":"/demo "} –

+0

@FreddyBoucher Siehe meine Bearbeitung. –

+0

Oh ich sehe, das ist schlau! Funktioniert aber immer noch nicht: {"timestamp": 1497569272670, "status": 415, "error": "Nicht unterstützter Medientyp", "exception": "org.springframework.web.HttpMediaTypeNotSupportedException", "message": "Inhalt Typ 'application/x-www-form-urlencoded; Zeichensatz = UTF-8' nicht unterstützt "," Pfad ":"/demo "} –

Verwandte Themen