简体   繁体   English

Spring @RequestBody映射问题-如何查看未映射的值?

[英]Spring @RequestBody mapping issue - how to see unmapped values?

I'm using a Spring Controller to transform JSON to Java via the @RequestBody annotation. 我正在使用Spring Controller通过@RequestBody批注将JSON转换为Java。 I'm debugging this but, as I've been through this before, I understand the JSON is not being mapped to my entity, but I'm very curious what the non mapped JSON is, from Java's perspective. 我正在调试它,但是,正如我之前所经历的那样,我知道JSON没有映射到我的实体,但是我非常好奇从Java的角度来看,未映射的JSON是什么。 Is there any way to make that visible in my Controller? 有什么方法可以使它在Controller中可见? Here's my controller method: 这是我的控制器方法:

    @PostMapping(path="/Add") // Map ONLY GET Requests
public @ResponseBody PurchaseRequestLineItem addNewPurchaseRequestLineItem (@RequestBody PurchaseRequestLineItem purchaseRequestLineItem) {

    purchaseRequestLineItemRepository.save(purchaseRequestLineItem);
    System.out.println("PurchaseRequestLineItem saved:  "+purchaseRequestLineItem);
    return purchaseRequestLineItem;
}

Here's my Entity: 这是我的实体:

    package com.prs.business.purchaserequest;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import com.fasterxml.jackson.annotation.JsonIgnore;

@Entity
@Table(name="purchaserequestlineitem")
public class PurchaseRequestLineItem {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;
//  @Column(name="purchaserequestid")
//  private int purchaseRequestID;
    @ManyToOne
    @JoinColumn(name="PurchaseRequestID")
    @JsonIgnore
    private PurchaseRequest purchaseRequest;
    private int productID;
    private int quantity;

    public PurchaseRequestLineItem() {
        id = 0;
        //purchaseRequestID = 0;
        purchaseRequest = null;
        productID = 0;
        quantity = 0;
    }

    public PurchaseRequestLineItem(PurchaseRequest inPR, int inPdtID, int inQty) {
        purchaseRequest = inPR;
        productID = inPdtID;
        quantity = inQty;
    }

//  public PurchaseRequestLineItem(int inPRID, int inPdtID, int inQty) {
//      purchaseRequestID = inPRID;
//      productID = inPdtID;
//      quantity = inQty;
//  }
//  
    public PurchaseRequestLineItem(int inID, PurchaseRequest inPR, int inPdtID, int inQty) {
        id = inID;
        purchaseRequest = inPR;
        productID = inPdtID;
        quantity = inQty;
    }

//  public PurchaseRequestLineItem(int inID, int inPRID, int inPdtID, int inQty) {
//      id = inID;
//      purchaseRequestID = inPRID;
//      productID = inPdtID;
//      quantity = inQty;
//  }
//
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }

//  public int getPurchaseRequestID() {
//      return purchaseRequestID;
//  }
//
//  public void setPurchaseRequestID(int purchaseRequestID) {
//      this.purchaseRequestID = purchaseRequestID;
//  }
//
    public PurchaseRequest getPurchaseRequest() {
        return purchaseRequest;
    }
    public void setPurchaseRequest(PurchaseRequest purchaseRequest) {
        this.purchaseRequest = purchaseRequest;
    }
    public int getProductID() {
        return productID;
    }
    public void setProductID(int productID) {
        this.productID = productID;
    }
    public int getQuantity() {
        return quantity;
    }
    public void setQuantity(int quantity) {
        this.quantity = quantity;
    }

    @Override
    public String toString() {
        return "\npurchaseRequestLineItem [id=" + id + ", purchaseRequest=" + purchaseRequest + ", productID="
                + productID + ", quantity=" + quantity + "]";
    }

}

And my line from my properties file: spring.jackson.property-naming-strategy=UPPER_CAMEL_CASE 我的属性文件中的行是:spring.jackson.property-naming-strategy = UPPER_CAMEL_CASE

You can just use String to see request body as is 您可以使用String原样查看请求正文

@PostMapping(path="/Add") // Map ONLY GET Requests
public @ResponseBody PurchaseRequestLineItem addNewPurchaseRequestLineItem (@RequestBody String json) {
    // do whatever you want
}

Assuming that you use Jackson JSON mapping, as noted by @gtosto, accept the @RequestBody as a String and perform the conversion from String to JSON yourself. 假设您使用@gtosto指出的杰克逊JSON映射,将@RequestBody接受为String并自己执行从String到JSON的转换。

Do that (the translation thing) using an ObjectMapper. 使用ObjectMapper进行翻译。

Here is a simple method for that: 这是一个简单的方法:

public <Type> Type getInputJson(
    final String trackId,
    final Class<Type> inputJsonClass,
    final String inputJsonString)
{
    Type returnValue;

    try
    {
        returnValue = objectMapper.readValue(
            inputJsonString,
            inputJsonClass);
    }
    catch (IOException exception)
    {
        logger.warn(
            "exception while converting inputJson.  inputJsonClass.name: {}, inputJsonString: {}",
            inputJsonClass.getName(),
            inputJsonString);

        returnValue = null;
    }

    return returnValue;
}

Edit removing my junk (CoreUtil). 编辑删除我的垃圾(CoreUtil)。 Also, I use logback, so my log strings might not match yours. 另外,我使用logback,所以我的日志字符串可能与您的日志字符串不匹配。

A second option is to accept an @RequestBody parameter like you are currently doing and add a WebRequest or HttpServletRequest parameter. 第二种选择是像您当前正在接受的那样接受@RequestBody参数,并添加WebRequestHttpServletRequest参数。 These give you access to the entire request (including the request body). 这些使您可以访问整个请求(包括请求主体)。

Note Using the ObjectMapper does not make the unmapped stuff available to you, it just does the json -> object mapping. 注意使用ObjectMapper不会使未映射的内容可供您使用,而只会进行json->对象映射。 You need to examine the inputJsonString to find the unmapped values. 您需要检查inputJsonString来找到未映射的值。

I use the following 3 jackson dependencies: 我使用以下3个Jackson依赖项:

    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-annotations</artifactId>
        <version>${jackson.version}</version>
    </dependency>

    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
        <version>${jackson.version}</version>
    </dependency>

    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>${jackson.version}</version>
    </dependency>

and the following property: 和以下属性:

    <jackson.version>2.2.3</jackson.version>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM