我有一個控制器發回一個JSON有效載荷Spring MVC的準備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視圖(本地主機:8080/{「merchantList」:[「Apple」,「Google」]}。jsp)
如何停止並返回JSON負載?
你可以在RequestMapping之上添加這個@RestController嗎? –
@georgesvan工作! –
不錯。請隨時在下面驗證我的答案 –