doudou5421 2017-03-30 06:09
浏览 875

将RequestBody json转换为Object - Spring Boot

I am a begineer in java development but has previous experience on programming languages like PHP and Python. So little confused on how to proceed on spring boot with the development.

I am developing a rest API which has the following request

{
  "key":"value",
  "key1":"value1",
  "platform_settings":[
      {"key":"value"}
  ]
}

What I did

I created a RestController which accepts the http request and created a function for the resource

public Share share(@RequestBody final Share share) { 
        LOGGER.debug("This is the request", share);
        return share; //
}

Question 1 : If it was any other programming language like PHP or Python, there will be helper function which will accept the json request and convert it to object which I can easily work on.

In python it is as simple as

import json
import requests

response = requests.get(...)
json_data = json.loads(response.text)
//can work on json_data anyway I want.

But in java, I will have to create a POJO class, or have jackson/JPA entity as dependency which will map the request to a Class (Which I should predefine with the requests).

Is there any better way I can do this? For every request I make, I will have to create a Class which the request can be mapped to and I will have to define the class

Entity

package com.payunow.socialsharemodule.models;

import java.util.Map;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;


import javax.persistence.Id;


@Entity
public class Share {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    private String key;
    private String key1;
    private Map<String,String> platform_settings;

    public Share(String name, String description,Map<String,String> platform_settings) {
        this.key = key;
        this.key1 = key1;
        this.platform_settings = platform_settings;
    }

    //for JPA
    public Share() {}

    public String getKey() {
        return key;
    }

    public String getKey1() {
        return key1;
    }

    public Map<String,String> getPlatform_settings() {
        return platform_settings;
    }

}

For every request I make, I will have to create a class defining all its variables inside. Is this the only way to do this?

  • 写回答

1条回答 默认 最新

  • dqwh1205 2017-03-30 08:24
    关注

    You need to have Jackson dependecy for coversion of json to java object. But spring provides it by default, so you don't have to add it explicitly.

    You don't need a JPA Entity. This is needed only when you want to store the recieved data into database.
    Just to recieve the request you don't have to create a separate pojo class. Look at this code

       @PostMapping("/json")
        public JSONObject getGeneric(@RequestBody String stringToParse){
            JSONParser parser = new JSONParser();
            JSONObject json = null;
            try {
                json = (JSONObject) parser.parse(stringToParse);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            return json;
        }   
    

    As you can see here it takes a string as a request and converts it into a generic JSONObject. So basically you can pass any json to this endpoint.

    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器