我使用Spring Controller通过@RequestBody注释将JSON转换为Java。我正在调试这个,但是,正如我以前经历过的那样,我理解JSON没有映射到我的实体,但我非常好奇非映射的JSON是什么,来自Java&# 39;观点。有没有办法让我的控制器可见?这是我的控制器方法:
@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;
}
这是我的实体:
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 + "]";
}
}
我的属性文件中的行: spring.jackson.property命名策略= UPPER_CAMEL_CASE
答案 0 :(得分:2)
您可以使用Error: digitalocean_volume.foovolume: "droplet_ids": this field cannot be set
按原样查看请求正文
String
答案 1 :(得分:0)
假设您使用Jackson JSON映射,
正如@gtosto所说,
接受@RequestBody
作为String并自行执行从String到JSON的转换。
使用ObjectMapper做那个(翻译事物)。
这是一个简单的方法:
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;
}
编辑删除我的垃圾(CoreUtil)。 此外,我使用logback,因此我的日志字符串可能与您的不匹配。
第二个选项是接受您当前正在执行的@RequestBody
参数,并添加WebRequest
或HttpServletRequest
参数。
这些可以让您访问整个请求(包括请求正文)。
注意使用ObjectMapper
不会使未映射的内容可用,它只是json - &gt;对象映射。您需要检查inputJsonString
以查找未映射的值。
我使用以下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>
以及以下属性:
<jackson.version>2.2.3</jackson.version>