如何在Spring Boot中读取带有标头和正文的JSON

塞萨尔·贾斯托(Cesar Justo):

早上好,我有一个小查询,我正在用Spring Boot进行一个小型Web服务休息,问题是它工作正常,并且其他所有操作(如下所示)接收到一个参数并返回一个基于Stored的响应数据库中的过程:

在此处输入图片说明

但是现在我更改了请求,它包括标头和正文,如下所示:

{
  "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"
      }
   }
}
}

当执行它给我一个找不到移动设备的答案时,当它无法读取移动设备参数或将其发送为空时,这是默认响应

在此处输入图片说明

我的密码

主班

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

控制者

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

}

Dao

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<DriverBonificados, Integer> {


    @Query(nativeQuery = true,value = "call ValidacionClienteBonificado(:movil)")
    ResponseVo validarClienteBonifiado(@Param("movil") String pMovil);

}

实体

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

}

接口响应存储过程

package com.app.validate.entity;

public interface ResponseVo {

    String getCode();
    String getResult();
}

在此处输入图片说明

您如何做才能读取标头和正文的Json?我是春季靴的新手

更新

According to what Silverfang said, I created the classes said by him, but I get an error that I describe next:

BodyRequest.java

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

}

HeaderRequest.java

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

}

RequestBodyDemo.java

package com.app.validate.controller;

public class RequestBodyDemo {

    private ValidateClientRequest ValidateClient;

    public ValidateClientRequest getValidateClient() {
        return ValidateClient;
    }

    public void setValidateClient(ValidateClientRequest validateClient) {
        ValidateClient = validateClient;
    }

}

ValidateClientRequest

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

}

My Controller (Update)

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

}

The error I get:

在此处输入图片说明

在此处输入图片说明

Silverfang :

From what I understand you have changed the request format and now want the same request body to work for the same controller.

I think you were trying to add the fields to the header. What you are doing here is not the right way to do it. It should goes to header section rather than in the body section of the Postman app. But doing so, you will have to specify these header separately as these are custom headers which will be a lot of work.

Answer to your question

Going by what you were trying to do. Since now you have changed the request body. You will have to make changes in the controller class too. Now it will require three classes If you want to do it in a modular way.

The first class will be BodyRequest.java

private string validateClientRequest;
private string movil;

The next class will be HeaderRequest.java

private string country;
private string lang;
private string entity;
private string system;
private string subsystem;
private string operation;
private Date timestamp;
private string msgType;

Next class will be ValidateClientRequest.java

private HeaderRequest Header;
private BodyRequest Body;

Now for the RequestBodyDemo class;

private ValidateClientRequest ValidateClient;

Note : Use appropriate Getter and setter along with @JsonProperty if you are masking the input request data.

Once these things are done. In your controller Instead of using Entity in @RequestBody Use the class RequestBodyDemo. Once that is done. Just try printing the values just to check whether you are getting them right or not. Then use getter for fetching any value that you need.

Edit :

  public ResponseVo ValidateClient(@RequestBody  RequestBodyDemo req) {
        System.out.println(req.getValidateClient().getBodyrequest().getMovil()); 

        return dao.validarClienteBonifiado(req.getValidateClient().getBodyrequest().getMovil()); 
    }

Note : Use appropriate getter method here.

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在凌空库Android中读取响应标头的状态代码和响应正文

如何使用PHP(cURL)中的post方法在带有标头的正文中发送JSON数据

rspec如何测试带有正文和标头的http调用

邮递员-如何查看带有标头和正文数据并替换了变量的请求

如何使用cURL发送带有正文,标头和HTTP参数的POST?

如何在Apache Drill中读取带有标头的文件

如何限制WAI中请求正文和标头的大小?

如何将带有复合标头的.csv读取到xarray DataArray中(使用熊猫)

如何在Java REST保证的HTTP请求中打印请求正文和标头?

虚函数如何在标头,正文和派生类中工作?

如何在Spring Boot中从资源读取JSON文件

如何使用带有标头和.so文件的库?

如何在Spring Boot中在index.html上设置cookie或标头

如何在Java Spring Boot中从请求标头获取承载令牌?

如何在Spring Boot中将Cache-Control标头添加到静态资源中?

如何通过带有标头的齐射发布JSON请求?

使用 Spring Boot 在带有 JSON 正文的 POST 请求中接收空参数

如何在Spring'HandlerMethodArgumentResolver'中多次读取请求正文?

如何在带有参数和标头的Android Volley库中使用POST请求?

如何在骆驼路线Spring DSL中设置JMSCorrelationId和JMSReplyTo标头

如何在Spring MVC中基于http请求标头启用json的动态漂亮打印?

在Spring MVC测试中访问请求正文和请求标头

如何在单独的文件中读取具有标头定义的CSV文件?

如何在golang中从http请求中读取标头?

如何在React Native应用程序中获取带有标头的api(POST)

如何在 Angular 7 中的 get 请求中发送带有标头的 apikey

如何在带有pyhton中的标头的数据框中转换汇总输出?

如何在Flutter中滚动带有粘性标头的堆叠式容器?

如何在 Spring Boot 中使用 OpenAPI 3 从“响应”和“请求正文”中隐藏“模式”?