aw123
aw123

Reputation: 273

Why is my value after restTemplate.postforEntity is null

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;
    }

Upvotes: 0

Views: 2374

Answers (1)

Eklavya
Eklavya

Reputation: 18430

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;
    ...
}

Upvotes: 2

Related Questions