【发布时间】:2018-02-25 15:57:36
【问题描述】:
我有一个控制器来发回 JSON 有效负载
@RequestMapping(value = "/MerchantMonitoringAPI", method = RequestMethod.GET,produces = "application/json")
public String MerchantMonitoring() {
ApplicationContext context =
new ClassPathXmlApplicationContext("Spring-Module.xml");
TopMerchantsDAO topMerchantsDAO = (TopMerchantsDAO) context.getBean("topMerchantsDAO");
TopMerchants topMerchants = topMerchantsDAO.retrieveMerchantList();
for(String temp:topMerchants.getMerchantList())
{
System.out.println(temp);
}
Gson gson = new Gson();
Type type = new TypeToken<TopMerchants>() {}.getType();
String jsonPayload = gson.toJson(topMerchants, type);
System.out.println(jsonPayload);
return jsonPayload;
}
它试图将我重定向到页面名称为 JSON (localhost:8080/{"merchantList":["Apple","Google"]}.jsp) 的视图
如何停止并返回 JSON 负载??
【问题讨论】:
-
你能把这个 @RestController 添加到 RequestMapping 的顶部吗?
-
@georgesvan 成功了!
-
不错。随意在下面验证我的答案
标签: json spring spring-mvc gson