为什么我的值在restTemplate.postforEntity之后是null

Why is my value after restTemplate.postforEntity is null

我在 Springboot 中使用 RESTtemplate 进行 POST-Call,但很清楚,我正在尝试 POST 这个 JSON:

{
  "MClass": "110",
  "param": "5"
}

我的服务看起来像:

package com.example.workflow;

import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;

@Service
public class CallServicen {
    private List<Calln> callList = new ArrayList<>();


    public List<Calln> getAllCallList() {
        return callList;
    }

    public void addCall(Calln calln) {
        callList.add(calln);
    }
}

我的控制器是:

package com.example.workflow;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class CallControllern {

    @Autowired
    private CallServicen callServicen;

    @GetMapping("/calls")
    public List<Calln> getAllCalls(){
        return callServicen.getAllCallList();
    }

    @PostMapping(value = "/calls")
    public void addCall(@RequestBody Calln calln){
        callServicen.addCall(calln);
    }
}

我的模型 Class 是这样的:

package com.example.workflow;

public class Calln {
    private String mclass;
    private String param;

    public Calln(String maschine, String param) {
        this.mclass = maschine;
        this.param = param;
    }

    public Calln(){}

    public String getMclass() {
        return mclass;
    }

    public void setMclass(String mclass) {
        this.mclass = mclass;
    }

    public String getParam() {
        return param;
    }

    public void setParam(String param) {
        this.param = param;
    }
}

我执行此 POST-调用 Class/Method:

public void post() {
        JsonParser parser = new JsonParser();
        JSONObject objFromFile = parser.parseJSONfromFilePathnew("json/Pressen.json");

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        JSONObject jsonObject= new JSONObject();
        //Http Entity made from Object from File
        HttpEntity<String> request = new HttpEntity<String>(objFromFile.toString(), headers);
        System.out.println("Headers:  "+request.getHeaders());
        System.out.println("Body:    "+request.getBody());
        ResponseEntity<Calln[]> response = restTemplate.postForEntity("http://localhost:8080/calls", request, Calln[].class);
    } 

我调试到最后一行代码,直到一切正常,requestbody 是 MClass = 110 和 param = 5,但在那之后它的 POSTing 只是 param = 5 对,对于 MClass 我得到空值。我不明白为什么,我的 GET 方法需要 POST,我可以使用 MClass 和参数。但我不明白为什么 MClass = null 但我的参数是正确的。

我这样写 JSON 解析器:

public JSONObject parseJSONfromFilePathnew(String filepath){
        JSONParser jsonParser = new JSONParser();
        JSONObject jsonObject = new JSONObject();
        try(FileReader read = new FileReader(filepath))
        {
            //Read JSON file
            jsonObject = (JSONObject)jsonParser.parse(read);
            String MClass = (String) jsonObject.get("MClass");
            String Param = (String) jsonObject.get("param");
            int param = Integer.parseInt(Param);
            int mclass = Integer.parseInt(MClass);
        } catch (FileNotFoundException | ParseException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return jsonObject;
    }

默认情况下,JSON 的 MClass 节点无法映射到 class 的 mclass 字段。 @JsonProperty 用于 Calln class 中的 mclass 字段。

public class Calln {
    @JsonProperty("MClass")
    private String mclass;
    ...
}