Raiyan Yahya Raiyan Yahya - 3 months ago 16
reST (reStructuredText) Question

Spring rest post service sending null values as variable

I am new to Spring MVC and I am exposing REST services. My

GET
services are working fine but the
POST
call from Postman is sending empty
null
values as fields.

Below is a sample REST service using various forms of
POST
annotations. I'm stuck on this for the past 3 days. Please help.

package com.cgi.ehr.rest;

import javax.ejb.EJB;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import com.cgi.ehr.services.dao.UserDetailsDAO;
import com.cgi.test.dto.InputDto;

@RestController
@RequestMapping("/bookmarks")
public class CheckingService {

@EJB
UserDetailsDAO userDetailsDAO;

@RequestMapping(value = "/bookmark", method = RequestMethod.GET)

public InputDto testM() {

return userDetailsDAO.testMethod();
}

@RequestMapping(value = "/testM2", method = RequestMethod.GET,headers="Accept=*/*", produces="application/json")
//@Produces(MediaType.APPLICATION_JSON)
public InputDto testM2() {
InputDto d = new InputDto("1","2");
return d;
}

@RequestMapping(value = "/bookmarkPost", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
//@ResponseBody
public InputDto testPost(InputDto inputDto) {
String age = inputDto.getTestAge();
String name = inputDto.getTestname();
InputDto dto = new InputDto();
dto.setTestAge(age + "gotback");
dto.setTestname(inputDto.getTestname() + "gotback");

return dto;
}


@RequestMapping(value = "/bookmarkPost1", method = RequestMethod.POST)
public InputDto testPost1(InputDto inputDto) {
String age = inputDto.getTestAge();
String name = inputDto.getTestname();
InputDto dto = new InputDto();
dto.setTestAge(age + "gotback");
dto.setTestname(inputDto.getTestname() + "gotback");

return dto;
}


@RequestMapping(value = "/person", method = RequestMethod.POST, headers = "Accept=application/xml, application/json")
public @ResponseBody InputDto addPerson(@RequestBody InputDto inputDto) {
return inputDto;
}

}
`

package com.cgi.test.dto;

import java.io.Serializable;

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class InputDto implements Serializable{


/**
*
*/
private static final long serialVersionUID = 1L;
private String testname;
private String testAge;
public String getTestname() {
return testname;
}
public void setTestname(String testname) {
this.testname = testname;
}
public String getTestAge() {
return testAge;
}
public void setTestAge(String testAge) {
this.testAge = testAge;
}
public InputDto() {
super();
}
public InputDto(String testname, String testAge) {

this.testname = testname;
this.testAge = testAge;
}


}


enter image description here

Answer

Your testPost method should look like below. Now you can use same request using Postman.

          @RequestMapping(value = "/bookmarkPost", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE,consumes = MediaType.APPLICATION_JSON_VALUE)
          public @ResponseBody InputDto testPost(@RequestBody InputDto inputDto) {
               String age = inputDto.getTestAge();
               String name = inputDto.getTestname();
               InputDto dto = new InputDto();
               dto.setTestAge(age + "gotback");
               dto.setTestname(name + "gotback");
               return dto;
    }