1

I am developing a platform based on the micro services architecture (JAX-RS) and a nodeJS API.

I have a problem adding an object to the database, because it always marks null by spring boot.

*Here is my REST controller code (JAX-RS):

@RequestMapping(value="/Add")
    public Actifs AjouterActifs( @RequestBody Actifs act){

        return Actif.saveT(act);
    }

*Here the code node API to add the object "Actifs":

app.post("/act/add",function (req,res) {

        var addActif = JSON.stringify(req.body);
        console.log("params: "+addActif);

        try {
            http.get(url+"/Add",+addActif, function (response) { //problem is here "addActif is null"
                var dataJson ='';
                response.on('data',function(data){
                    dataJson += data;
                });
                response.on('end',function(){
                    try
                    {
                        var addAct = JSON.parse(dataJson);
                    }
                    catch(err) {
                        console.log('erreur de retourner l\'actif  -->', +err);
                    }
                    res.json(addAct);
                });
            });
        }
        catch(e) {
            console.log("erreur d'ajouter les info d'actif -->", +e);
        }
    });

*Postman: enter image description here

I get this error:

org.springframework.http.converter.HttpMessageNotReadableException: Required request body is missing:

How to avoid a null object passing from node JS to the JAX-RS service ?

thank you for helping me,

emile01
  • 89
  • 1
  • 3
  • 15

2 Answers2

0

You are sending the actif to be added as a query parameter

http.get(url+"/Add?act="+addActif, function (response) {
   ...
}

But your SpringMVC Endpoint expects to find the Actif object in the request body

@RequestMapping(value="/Add")
public Actifs AjouterActifs( @RequestBody(required=false) Actifs act) {
   ...
}

Option 1: Use @RequestParameter("act") Actifs act and register an editor to parse the object from the query parameter string (see this question).

Option 2: Actually send the Actif json as the request body, e.g. by performing a POST request to url + "/Add" instead of a GET. You will have to use http.request to implement that.

Furthermore I would suggest to use @RequestBody (without required=false). That ensures that the parameter must be non-null and lets the application fail fast if that is not the case.

Pyranja
  • 3,529
  • 22
  • 24
  • Thank you for your reply Sir, I tried to change the code as you have: `Http.get (url +" / add ", + addActif, function (response)` AND `public actifs (@RequestBody Actif act)` but when I removed `@RequestBody (required = false)`   I have this problem-> "HttpMessageNotReadableException: Required request body is missing", thanks –  emile01 Mar 13 '17 at 13:23
  • You have to choose one of the outline options. Note that the exception accurately describes your problem: There is no request body. Either send a request body from nodejs or parse the expected object from the query parameter. – Pyranja Mar 13 '17 at 14:40
  • I am trying to send the received object from the url of nodejs to the web service JAX-RS. But I still the object is null. I put the last update of the code. –  emile01 Mar 13 '17 at 14:47
0

I solved the problem by changing the code like this

app.post("/act/add",function (req,res) {

        var addActif = JSON.parse(req.body); //parse object
        console.log("params: "+addActif);

        try {
            http.get(url+"/Add",addActif, function (response) { // delete '+'
                var dataJson ='';
                response.on('data',function(data){
                    dataJson += data;
                });
                response.on('end',function(){
                    try
                    {
                        var addAct = JSON.parse(dataJson);
                    }
                    catch(err) {
                        console.log('erreur de retourner l\'actif  -->', +err);
                    }
                res.json(addAct);
            });
        });
    }
    catch(e) {
        console.log("erreur d'ajouter les info d'actif -->", +e);
    }
});
emile01
  • 89
  • 1
  • 3
  • 15