2017-10-19 1 views
-1

스프링과 Gson이 내 객체를 서버에 POST하는 데 문제가 있습니다. 데이터가 포함되어 있더라도 비어있는 결과가 반환됩니다.Spring RESTful 웹 서비스 POST 객체

이 코드는 RESTful 웹 서비스부터입니다 :

@RequestMapping("/summe") 
    public @ResponseBody String summe(ArrayList<ServiceSettingsListItem> list) { 
    return "Greeting.summe() = <" + list + ">" + list.size(); 
} 

내 클라이언트 :

{"url":"http://localhost:8080/summe", 
"test":123, 
"items":[ 
{"name":"name1","value":5}, 
{"name":"name1","value":6} 
] 
} 

: 여기

private static Gson gson = new GsonBuilder().create(); 

public static void main(String[] args) throws IOException { 
    String fileContent = new String(Files.readAllBytes(new 
File("demo.json").toPath())); 
    ServiceSettings serviceSettings = gson.fromJson(fileContent, 
ServiceSettings.class); 
    String baseUrl = serviceSettings.getUrl(); 
    RestTemplate restTemplate = new RestTemplate(); 
    String result = restTemplate.postForObject(baseUrl, serviceSettings, String.class); 
    System.out.println(result); 
} 

public static class ServiceSettings { 

    @Override 
    public String toString() { 
     return "ServiceSettings [url=" + url + ", test=" + test + ", items=" 
    + items + "]"; 
    } 

    private String url; 
    private int test; 
    private ArrayList<ServiceSettingsListItem> items = new ArrayList<>(); 

    public String getUrl() { 
     return url; 
    } 

    public void setUrl(String url) { 
     this.url = url; 
    } 

    public int getTest() { 
     return test; 
    } 

    public void setTest(int test) { 
     this.test = test; 
    } 

    public ArrayList<ServiceSettingsListItem> getItems() { 
     return items; 
    } 

    public void setItems(ArrayList<ServiceSettingsListItem> items) { 
     this.items = items; 
    } 

} 

public static class ServiceSettingsListItem { 
    private String name; 
    private int value; 

    public String getName() { 
     return name; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 

    public int getValue() { 
     return value; 
    } 

    public void setValue(int value) { 
     this.value = value; 
    } 

} 

}

그리고 내 JSON입니다 나는 다시 얻을 것을 기대한다. 결과는 0이라고 표시됩니다.

답변

0

ServiceSettings 개체를 게시하는 대신 컨트롤러에서 페이로드가 목록이 될 것으로 예상하므로 items 요소를 게시해야합니다. 다음을 시도해보십시오.