英文:
Why is my value after restTemplate.postforEntity is null
问题
我正在Spring Boot中使用RESTtemplate进行POST调用,但一切都很清楚,我试图POST这个JSON:
{
  "MClass": "110",
  "param": "5"
}
我的Service类如下:
package com.example.workflow;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
@Service
public class CallService {
    private List<Calln> callList = new ArrayList<>();
    public List<Calln> getAllCallList() {
        return callList;
    }
    public void addCall(Calln calln) {
        callList.add(calln);
    }
}
我的Controller类如下:
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 CallController {
    @Autowired
    private CallService callService;
    @GetMapping("/calls")
    public List<Calln> getAllCalls(){
        return callService.getAllCallList();
    }
    @PostMapping(value = "/calls")
    public void addCall(@RequestBody Calln calln){
        callService.addCall(calln);
    }
}
我的Model类如下:
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调用:
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);
}
我调试到了代码的最后一行,在那里一切都没问题,请求体是MClass = 110和param = 5,但之后它只是POST了param = 5,对于MClass,我得到了null。我不明白为什么,我需要这个POST来使用MClass和param进行我的GET方法,但我不明白为什么MClass = null,而我的param被正确地POST了。
我这样编写了JSON解析器:
public JSONObject parseJSONfromFilePathnew(String filepath){
    JSONParser jsonParser = new JSONParser();
    JSONObject jsonObject = new JSONObject();
    try(FileReader read = new FileReader(filepath))
    {
        //读取JSON文件
        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;
}
英文:
Im using RESTtemplate in Springboot for a POST-Call, but its all clear, I am trying to POST this JSON:
{
  "MClass": "110",
  "param": "5"
}
My Service looks like:
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);
    }
}
My Controller is:
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);
    }
}
My Modell Class is this:
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;
    }
}
I execute this POST-Call in a 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);
    } 
I debugged till this last row of Code, and till there all is ok, requestbody is MClass = 110 and param = 5, but after that its POSTing just the param = 5 right, for MClass I get null. I don't understand why, I need that POST for my GET-Method that I can work with the MClass and param. but I dont get it why MClass = null but my param is right posted.
I wrote my JSON Parser this way:
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;
    }
答案1
得分: 2
默认情况下,JSON 的 MClass 节点无法映射到类的 mclass 字段。在 Calln 类中,使用 @JsonProperty 注解来标记 mclass 字段。
public class Calln {
    @JsonProperty("MClass")
    private String mclass;
    ...
}
英文:
By default MClass node of JSON can't map into mclass field of class. Use @JsonProperty for mclass field in Calln class.
public class Calln {
    @JsonProperty("MClass")
    private String mclass;
    ...
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论