[英]Convert Spring MVC to Spring fullREST JSON application
我在Java Spring中制作類似於以下內容的應用程序: Java Blog Aggregator
該應用程序的作者使用Spring MVC和常見的http請求。 將控制器重塑為使用AJAX的完全休息應用程序的快速方法嗎? 我不知道從哪里開始。 我想發送JSON。 我現在沒有自己的代碼,因為我才剛剛開始。
控制器示例:
package cz.jiripinkas.jba.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import cz.jiripinkas.jba.service.ItemService;
@Controller
public class IndexController {
@Autowired
private ItemService itemService;
@RequestMapping("/index")
public String index(Model model) {
model.addAttribute("items", itemService.getItems());
return "index";
}
}
謝謝你的幫助。
是的,您可以按照以下步驟操作:
public String index(Model model)
方法轉換為 public void index(Model model, HttpServletResponse response)
。 return "index";
因為現在您的方法是void
類型。 JSONObject
類型的對象 JSONObject
類型的名為returnJSONObj
的變量,則可以將其轉換為String,如下所示: String returnJSONStr = returnJSONObj.toString()
response.setContentType("application/json"); response.getWriter.println(returnJSONStr);
這將使您的方法返回一個JSON字符串,您可以在REST API中使用它。
添加了示例代碼以供參考
package cz.jiripinkas.jba.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import cz.jiripinkas.jba.service.ItemService;
@Controller
public class IndexController {
@Autowired
private ItemService itemService;
@RequestMapping("/index")
public void index(Model model, HttpServletResponse response) {
List items = itemService.getItems();
String returnJSONStr = createJSONStr(items);
response.setContentType("application/json");
response.getWriter().println(returnJSONStr); //this method throws IOException
}
private String createJSONStr(List items) {
JSONObject json = new JSONObject();
for(Item item: items) { //For each item
json.put("property1", item.getProperty1()); //replace by your own properties
json.put("property2", item.getProperty2());
}
return json.toString();
}
}
您可以通過以下方法使它成為REST Web服務:
1)用@RestController替換@Controller。
2)將@RequestMapping(“ / index”)更改為:
@RequestMapping(value="/index",
method=RequestMethod.GET,
produces=MediaType.APPLICATION_JSON_VALUE)
3)如果您打算在此索引Web服務中接收一些有效負載,則將RequestMethod.GET
替換為RequestMethod.POST
並將索引函數的參數更改為某個DTO對象,該對象將與您想要接收的json映射並使用@RequestBody
注釋(如果使用@RestController,這是可選的)。
編輯:
4)如果要返回JSON對象,只需創建一個屬性名稱與鍵相同的bean:
class User {
private String id;
private String name;
public void setName(String name){
this.name=name;
}
public void getName(){
return this.name;
}
public void getId(){
return this.id;
}
public void setId(String id){
this.id=id;
}
}
並從方法中返回此對象,如下所示:
@RequestMapping(value="/index",
method=RequestMethod.GET,
produces=MediaType.APPLICATION_JSON_VALUE)
public User index() {
User user = new User();
user.setId("1");
user.setName("Test");
return user;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.