簡體   English   中英

將Spring MVC轉換為Spring fullREST JSON應用程序

[英]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";
    }
}

謝謝你的幫助。

是的,您可以按照以下步驟操作:

  1. 下載一個JSON API jar,並將該庫包含在您的應用程序中。
    您可以在此處下載一個
  2. 將您的public String index(Model model)方法轉換為
    public void index(Model model, HttpServletResponse response)
  3. 刪除行return "index"; 因為現在您的方法是void類型。
  4. 使用剛添加的庫構造一個JSON。 您最終將擁有JSONObject類型的對象
  5. 獲得此對象后,將其轉換並存儲為字符串。 例如,如果您有一個名為JSONObject類型的名為returnJSONObj的變量,則可以將其轉換為String,如下所示:
    String returnJSONStr = returnJSONObj.toString()
  6. 最后,您將輸出寫入JSON字符串:-
    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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM