如何使用 spring resttemplate 正确使用 JSON 响应?

How to consume JSON response with spring restemplate properly?

我有一个 Spring MVC Rest 服务 return JSON 值,我有 8 行,这是我的 JSON

[
{"no":"1","date":"2015-03-30","grandtotal":699618,"diskon":699618},
{"no":"2","date":"2015-03-30","grandtotal":1867949,"diskon":1867949},
{"no":"3","date":"2015-03-27","grandtotal":2190909,"diskon":2190909},
{"no":"4","date":"2015-03-26","grandtotal":8616120,"diskon":8616120},
{"no":"5","date":"2015-03-26","grandtotal":1095455,"diskon":1095455},
{"no":"6","date":"2015-03-24","grandtotal":938961,"diskon":938961},
{"no":"7","date":"2015-03-24","grandtotal":5603848,"diskon":5603848},
{"no":"8","date":"2015-03-20","grandtotal":3735899,"diskon":3735899}
]

我在尝试什么..这是我的控制器。 SpringRestController.java 杰克逊方式:

@RequestMapping(value = "/view", method = RequestMethod.GET)
public String initCreationForm(Map<String, Object> model) {

    String url = "http://localhost:8080/SpringServiceJson/view/";

    RestTemplate restTemplate = new RestTemplate();
    TotalDiscList totaldisc = restTemplate.getForObject(url, TotalDisc.class);


    model.put("DiscValue",totaldisc);
    return "salesorders/totalDisc";
}

Gson 方式:

public String initCreationForm(Map<String, Object> model) {

String url = "http://localhost:8080/SpringServiceJson/view/";

Gson gson = new Gson();

Collection<TotalDisc> totaldisc = gson.fromJson(url, PiutangListJson.class);



    model.put("DiscValue",totaldisc);
    return "salesorders/totalDisc";
}

我错过了什么?它总是给我这个错误 "Could not extract response: no suitable HttpMessageConverter found for response type [class [Lorg.springframework.samples.my.model.TotalDiscList;] and content type [application/json]"

这是我的对象TotalDiscList.java

public class TotalDiscList {

    private String no;
    @DateTimeFormat(pattern="dd-MM-yyyy")
    private Date date;
    private long grandtotal;
    private long diskon;

//getter setter skipped
}

我应该 return List<TotalDiscList> totaldisc = restTemplate.getForObject(url, List<TotalDisc>.class); 对吧? 我如何正确地做到这一点?

编辑

您需要为 RestTemplate

提供消息转换器
restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
restTemplate.getMessageConverters().add(new StringHttpMessageConverter());

然后尝试使用数组,例如:

TotalDiscList[] totaldisc = restTemplate
                    .getForObject(url, TotalDiscList[].class);

如果您有一个 servlet-context.xml,您可以在那里添加消息转换器,如下所示:

 <beans:bean
        class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
        <beans:property name="messageConverters" ref="jsonMessageConverter" />
    </beans:bean>

    <beans:bean id="jsonMessageConverter"
        class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" />

我也在做同样的事情并且有效。

您可以配置一个 json 消息转换器:

<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
    <property name="messageConverters">
       <list>
            <ref bean="jsonConverter" />
       </list>
    </property>
</bean> 

然后,您只需注释您的方法即可:

   @RequestMapping(value = "/view", method = RequestMethod.POST)
   public TotalDiscList[] createDiscList(
         @RequestBody TotalDiscList[] object) {

   }

这会导致消息转换器尝试转换为给定的 class。