【问题标题】:Reading json data in spring controller sent by angular 2 post在 angular 2 post 发送的 spring 控制器中读取 json 数据
【发布时间】:2017-07-28 15:11:13
【问题描述】:

我实际上找到了解决方案,但我认为有更简单的方法。我正在尝试使用 angular2 发布 json 数据并尝试在 spring 控制器中读取它。

类是

export class EmovieCat{

    id:String = "test";
    rev:String;
    dataModelVersion:number = 99;

}

角度 2 中的邮政编码

 this.emv  = new EmovieCat();
        this._http.post(this._url,JSON.stringify(this.emv)).subscribe(response =>{
            console.log(response.json());
        });

我得到这样的 json 字符串。

BufferedReader br = null;
        try {
            br = new BufferedReader(new InputStreamReader(this.req.getInputStream()));
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        String json = "";

        if(br != null){
            try {
                json = br.readLine();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

这样我可以得到 json 字符串。 在此先感谢

【问题讨论】:

    标签: json spring angular


    【解决方案1】:

    你应该这样的控制器:

    @RequestMapping(method ={ RequestMethod.POST }, value ={ "/save/info" }, produces = { "application/json" })
    public ResponseEntity<String> storeRedirectUrl(@RequestBody EmovieCat emc)
    {
        //Here you have the emc object that is the JSON reversed in Java Object
    }
    

    在这种情况下,Spring 能够通过使用 jackson marshaller API 创建 Emoviecat 对象,您只需关心业务逻辑

    【讨论】:

      猜你喜欢
      • 2019-04-12
      • 1970-01-01
      • 2018-01-18
      • 1970-01-01
      • 2013-07-28
      • 2018-04-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多