title: How can I read a JSON with header and body with spring boot
body: <p>Good morning I have a small query, I am doing a small web service rest with spring boot, the issue is that it is working fine and everything else, as I am doing as follows, receives a parameter and returns a response based on a Stored Procedue in the database:</p>
  
  <p><a href="https://i.stack.imgur.com/gWg9W.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/gWg9W.png" alt="enter image description here"></a></p>
  
  <p>But now I have changed the request, and it is including header and body, like the following:</p>
  
  <pre><code>{
    "ValidateClient": {
      "Header": {
          "country": "VE",
          "lang": "ES",
          "entity": "TMVE",
          "system": "76",
          "subsystem": "APP",
          "operation": "ValidateClient",
          "timestamp": "2019-10-23T08:48:08.474Z",
          "msgType": "REQUEST"
        },
      "Body": {
        "validateClientRequest": {
          "movil": "04141734272"
        }
     }
  }
  }
  </code></pre>
  
  <p>Which when executing it gives me an answer of not found the mobile, it is a default response when it cannot read the mobile parameter or it is sent empty</p>
  
  <p><a href="https://i.stack.imgur.com/WApyE.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/WApyE.png" alt="enter image description here"></a></p>
  
  <p>My Code</p>
  
  <p>Main Class</p>
  
  <pre><code>package com.app.validate;
  
  import org.springframework.boot.SpringApplication;
  import org.springframework.boot.autoconfigure.SpringBootApplication;
  
  @SpringBootApplication
  public class ValidateClientApp {
  
      public static void main(String[] args) {
          SpringApplication.run(ValidateClientApp.class, args);
      }
  }
  </code></pre>
  
  <p>Controller</p>
  
  <pre><code>package com.app.validate.controller;
  
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.web.bind.annotation.PostMapping;
  import org.springframework.web.bind.annotation.RequestBody;
  import org.springframework.web.bind.annotation.RestController;
  
  import com.app.validate.dao.ValidateClientAppRepository;
  import com.app.validate.entity.DriverBonificados;
  import com.app.validate.entity.ResponseVo;
  
  @RestController
  public class ValidateClientAppController {
  
      @Autowired
      private ValidateClientAppRepository dao; 
  
      @PostMapping(value = "/ValidateClientApp",consumes = "application/json",produces="application/json") 
      public ResponseVo ValidateClient(@RequestBody DriverBonificados driver) {
          //System.out.println(driver.getMovil()); 
          return dao.validarClienteBonifiado(driver.getMovil()); 
      }
  
  }
  </code></pre>
  
  <p>Dao</p>
  
  <pre><code>package com.app.validate.dao;
  
  import org.springframework.data.jpa.repository.JpaRepository;
  import org.springframework.data.jpa.repository.Query;
  import org.springframework.data.repository.query.Param;
  import org.springframework.stereotype.Repository;
  
  import com.app.validate.entity.DriverBonificados;
  import com.app.validate.entity.ResponseVo;
  
  @Repository
  public interface ValidateClientAppRepository extends JpaRepository&lt;DriverBonificados, Integer&gt; {
  
  
      @Query(nativeQuery = true,value = "call ValidacionClienteBonificado(:movil)")
      ResponseVo validarClienteBonifiado(@Param("movil") String pMovil);
  
  }
  </code></pre>
  
  <p>Entity</p>
  
  <pre><code>package com.app.validate.entity;
  
  import java.util.Date;
  
  import javax.persistence.Entity;
  import javax.persistence.Id;
  import javax.persistence.Table;
  
  @Entity
  @Table(name="DriverBonificados")
  public class DriverBonificados {
  
      @Id
      private int id;     
      private String movil;
      private String contador;
      private Date fecha_driver;
      private Date fecha_alta;
      private Date fecha_fin;
      private Date codigo_transaccion;
      public int getId() {
          return id;
      }
      public void setId(int id) {
          this.id = id;
      }
      public String getMovil() {
          return movil;
      }
      public void setMovil(String movil) {
          this.movil = movil;
      }
      public String getContador() {
          return contador;
      }
      public void setContador(String contador) {
          this.contador = contador;
      }
      public Date getFecha_driver() {
          return fecha_driver;
      }
      public void setFecha_driver(Date fecha_driver) {
          this.fecha_driver = fecha_driver;
      }
      public Date getFecha_alta() {
          return fecha_alta;
      }
      public void setFecha_alta(Date fecha_alta) {
          this.fecha_alta = fecha_alta;
      }
      public Date getFecha_fin() {
          return fecha_fin;
      }
      public void setFecha_fin(Date fecha_fin) {
          this.fecha_fin = fecha_fin;
      }
      public Date getCodigo_transaccion() {
          return codigo_transaccion;
      }
      public void setCodigo_transaccion(Date codigo_transaccion) {
          this.codigo_transaccion = codigo_transaccion;
      }
  
  }
  </code></pre>
  
  <p>Interface Response Stored Procedue</p>
  
  <pre><code>package com.app.validate.entity;
  
  public interface ResponseVo {
  
      String getCode();
      String getResult();
  }
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/7sTCB.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/7sTCB.png" alt="enter image description here"></a></p>
  
  <p>How could you do to read the Json with header and body? I'm new to spring boot</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>According to what Silverfang said, I created the classes said by him, but I get an error that I describe next:</p>
  
  <p>BodyRequest.java</p>
  
  <pre><code> public class BodyRequest {
  
      private String validateClientRequest;
      private String movil;
  
      public String getValidateClientRequest() {
          return validateClientRequest;
      }
      public void setValidateClientRequest(String validateClientRequest) {
          this.validateClientRequest = validateClientRequest;
      }
      public String getMovil() {
          return movil;
      }
      public void setMovil(String movil) {
          this.movil = movil;
      }
  
  }
  </code></pre>
  
  <p>HeaderRequest.java</p>
  
  <pre><code>package com.app.validate.controller;
  
  import java.util.Date;
  
  public class HeaderRequest {
  
      private String country;
      private String lang;
      private String entity;
      private String system;
      private String subsystem;
      private String operation;
      private Date timestamp;
      private String msgType;
      public String getCountry() {
          return country;
      }
      public void setCountry(String country) {
          this.country = country;
      }
      public String getLang() {
          return lang;
      }
      public void setLang(String lang) {
          this.lang = lang;
      }
      public String getEntity() {
          return entity;
      }
      public void setEntity(String entity) {
          this.entity = entity;
      }
      public String getSystem() {
          return system;
      }
      public void setSystem(String system) {
          this.system = system;
      }
      public String getSubsystem() {
          return subsystem;
      }
      public void setSubsystem(String subsystem) {
          this.subsystem = subsystem;
      }
      public String getOperation() {
          return operation;
      }
      public void setOperation(String operation) {
          this.operation = operation;
      }
      public Date getTimestamp() {
          return timestamp;
      }
      public void setTimestamp(Date timestamp) {
          this.timestamp = timestamp;
      }
      public String getMsgType() {
          return msgType;
      }
      public void setMsgType(String msgType) {
          this.msgType = msgType;
      }
  
  }
  </code></pre>
  
  <p>RequestBodyDemo.java</p>
  
  <pre><code>package com.app.validate.controller;
  
  public class RequestBodyDemo {
  
      private ValidateClientRequest ValidateClient;
  
      public ValidateClientRequest getValidateClient() {
          return ValidateClient;
      }
  
      public void setValidateClient(ValidateClientRequest validateClient) {
          ValidateClient = validateClient;
      }
  
  }
  </code></pre>
  
  <p>ValidateClientRequest</p>
  
  <pre><code>package com.app.validate.controller;
  
  public class ValidateClientRequest {
  
      private BodyRequest Body;
      private HeaderRequest Header;
  
      public BodyRequest getBody() {
          return Body;
      }
      public void setBody(BodyRequest body) {
          Body = body;
      }
      public HeaderRequest getHeader() {
          return Header;
      }
      public void setHeader(HeaderRequest header) {
          Header = header;
      }
  
  }
  </code></pre>
  
  <p>My Controller (Update)</p>
  
  <pre><code>package com.app.validate.controller;
  
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.web.bind.annotation.PostMapping;
  import org.springframework.web.bind.annotation.RequestBody;
  import org.springframework.web.bind.annotation.RestController;
  
  import com.app.validate.dao.ValidateClientAppRepository;
  import com.app.validate.entity.DriverBonificados;
  import com.app.validate.entity.ResponseVo;
  
  @RestController
  public class ValidateClientAppController {
  
      @Autowired
      private ValidateClientAppRepository dao; 
  
      @PostMapping(value = "/ValidateClientApp",consumes = "application/json",produces="application/json") 
      public ResponseVo ValidateClient(@RequestBody  RequestBodyDemo req) {
          System.out.println(req.getValidateClient().getBody().getMovil());
  
          return dao.validarClienteBonifiado(req.getValidateClient().getBody().getMovil()); 
      }
  
  }
  </code></pre>
  
  <p>The error I get:</p>
  
  <p><a href="https://i.stack.imgur.com/OTGNP.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/OTGNP.png" alt="enter image description here"></a></p>
  
  <p><a href="https://i.stack.imgur.com/ayZkh.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ayZkh.png" alt="enter image description here"></a></p>