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

时间:2015-05-12 07:30:48

标签: json spring spring-mvc restful-url

我有一个返回JSON值的Spring MVC Rest服务,我有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";
}
我错过了什么?它总是给我这个错误 “无法提取响应:没有为响应类型找到合适的HttpMessageConverter [类[Lorg.springframework.samples.my.model.TotalDiscList;]和内容类型[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
}

我应该回复List<TotalDiscList> totaldisc = restTemplate.getForObject(url, List<TotalDisc>.class);对吗? 我怎么做到这一点?

3 个答案:

答案 0 :(得分:1)

如果你有一个servlet-context.xml,你可以在那里添加message-convertor,如下所示:

 <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" />

我也在做同样的工作。

答案 1 :(得分:0)

修改

您需要为filtered = [] previous_word = None for word in sentence.split(' '): if previous_word and synonymous(word, previous_word): continue else: filtered.append(word) previous_word = word ' '.join(filtered)

提供消息转换器
words = sentence.split(' ')
new_sentence = ' '.join(word for word, previous in zip(words, [None] + words)
                        if not synonymous(word, previous))

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

RestTemplate

答案 2 :(得分:0)

您可以配置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) {

   }

会导致邮件转换器尝试转换为给定的类。