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

huangapple go评论87阅读模式
英文:

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:

{
  &quot;MClass&quot;: &quot;110&quot;,
  &quot;param&quot;: &quot;5&quot;
}

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&lt;Calln&gt; callList = new ArrayList&lt;&gt;();


    public List&lt;Calln&gt; 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(&quot;/calls&quot;)
    public List&lt;Calln&gt; getAllCalls(){
        return callServicen.getAllCallList();
    }

    @PostMapping(value = &quot;/calls&quot;)
    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(&quot;json/Pressen.json&quot;);

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        JSONObject jsonObject= new JSONObject();
        //Http Entity made from Object from File
        HttpEntity&lt;String&gt; request = new HttpEntity&lt;String&gt;(objFromFile.toString(), headers);
        System.out.println(&quot;Headers:  &quot;+request.getHeaders());
        System.out.println(&quot;Body:    &quot;+request.getBody());
        ResponseEntity&lt;Calln[]&gt; response = restTemplate.postForEntity(&quot;http://localhost:8080/calls&quot;, 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(&quot;MClass&quot;);
            String Param = (String) jsonObject.get(&quot;param&quot;);
            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(&quot;MClass&quot;)
    private String mclass;
    ...
}

huangapple
  • 本文由 发表于 2020年8月10日 15:46:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/63336079.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定