尝试使用Spring启动MVC在post方法中传递List,我试图将一个json数组传递给一个带有List的rest api,但是它说Request主体格式不正确。
这是我传递的JSON格式
[{"quotation":100001,"customer":"ZOMRAN KHALED MEDICAL UNIT",
"currency":"Algerian dinar (DZD)","country":"Algeria","language":"English",
"product":"9100C","modality":"LCS","subModality":"ARC","services":"PM,CM",
"serviceType":"SWIFT","price":"1500","responseTime":"24H","createdTime":"24/04/2018"},
{"quotation":100002,"customer":"ZOMRAN KHALED MEDICAL UNIT",
"currency":"Algerian dinar (DZD)","country":"Algeria","language":"English",
"product":"9100C","modality":"LCS","subModality":"ARC","services":"PM,CM",
"serviceType":"SWIFT","price":"1500","responseTime":"24H","createdTime":"24/04/2018"}
]
这是我的Java控制器,我正在捕捉数组。
@RequestMapping(value = "/carePlanQuotationHistory", method = RequestMethod.POST, produces = {
MediaType.APPLICATION_JSON_VALUE, MediaType.TEXT_PLAIN_VALUE })
public @ResponseBody Response CarePlanQuotationHistory1(@RequestBody List<CarePlanHistoryRequest> carePlanQuotationHistory1) throws Exception {
int a;
try {
a = productService.CarePlanQuotationHistory1(carePlanQuotationHistory1);
} catch (Exception ex) {
return new Response("400", ex.getMessage());
}
return new Response("200", a);
}
这是我的pojo
package com.careplan.springbootstarter.request;
public class CarePlanHistoryRequest {
private String quotation;
private String customer;
private String currency;
private String country;
private String language;
private String product;
private String modality;
private String subModality;
private String services;
private String serviceType;
private String price;
private String responseTime;
private String createdTime;
public CarePlanHistoryRequest() {
super();
// TODO Auto-generated constructor stub
}
public CarePlanHistoryRequest(String quotation, String customer, String currency, String country, String language,
String product, String modality, String subModality, String services, String serviceType, String price,
String responseTime, String createdTime) {
super();
this.quotation = quotation;
this.customer = customer;
this.currency = currency;
this.country = country;
this.language = language;
this.product = product;
this.modality = modality;
this.subModality = subModality;
this.services = services;
this.serviceType = serviceType;
this.price = price;
this.responseTime = responseTime;
this.createdTime = createdTime;
}
public String getQuotation() {
return quotation;
}
public void setQuotation(String quotation) {
this.quotation = quotation;
}
public String getCustomer() {
return customer;
}
public void setCustomer(String customer) {
this.customer = customer;
}
public String getCurrency() {
return currency;
}
public void setCurrency(String currency) {
this.currency = currency;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public String getLanguage() {
return language;
}
public void setLanguage(String language) {
this.language = language;
}
public String getProduct() {
return product;
}
public void setProduct(String product) {
this.product = product;
}
public String getModality() {
return modality;
}
public void setModality(String modality) {
this.modality = modality;
}
public String getSubModality() {
return subModality;
}
public void setSubModality(String subModality) {
this.subModality = subModality;
}
public String getServices() {
return services;
}
public void setServices(String services) {
this.services = services;
}
public String getServiceType() {
return serviceType;
}
public void setServiceType(String serviceType) {
this.serviceType = serviceType;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
public String getResponseTime() {
return responseTime;
}
public void setResponseTime(String responseTime) {
this.responseTime = responseTime;
}
public String getCreatedTime() {
return createdTime;
}
public void setCreatedTime(String createdTime) {
this.createdTime = createdTime;
}
}
这是我们遇到的错误
{
"timestamp": 1526647767010,
"status": 400,
"error": "Bad Request",
"exception": "org.springframework.http.converter.HttpMessageNotReadableException",
"message": "Could not read document: Can not instantiate value of type [simple type, class com.careplan.springbootstarter.entity.Customer] from String value ('ZOMRAN KHALED MEDICAL UNIT'); no single-String constructor/factory method\n at [Source: java.io.PushbackInputStream@46f9d4f8; line: 1, column: 21] (through reference chain: java.util.ArrayList[0]->com.careplan.springbootstarter.entity.CarePlanQuotationHistory[\"customer\"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not instantiate value of type [simple type, class com.careplan.springbootstarter.entity.Customer] from String value ('ZOMRAN KHALED MEDICAL UNIT'); no single-String constructor/factory method\n at [Source: java.io.PushbackInputStream@46f9d4f8; line: 1, column: 21] (through reference chain: java.util.ArrayList[0]->com.careplan.springbootstarter.entity.CarePlanQuotationHistory[\"customer\"])",
"path": "/api/product/carePlanQuotationHistory"
}
这是我的pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>Entransys</groupId>
<artifactId>CarePlanConfig</artifactId>
<packaging>jar</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>CarePlanConfig Maven Webapp</name>
<url>http://maven.apache.org</url>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.5.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.12</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.12</version>
</dependency>
<dependency>
<groupId>net.sourceforge.jexcelapi</groupId>
<artifactId>jxl</artifactId>
<version>2.6.12</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-email</artifactId>
<version>1.3</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.5</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<executable>true</executable>
</configuration>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<executions>
<execution>
<id>copy-resources</id>
<phase>validate</phase>
<goals><goal>copy-resources</goal></goals>
<configuration>
<outputDirectory>${basedir}/target/classes/static/</outputDirectory >
<resources>
<resource>
<directory>${basedir}/../care-plan-client/dist</directory >
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
答案 0 :(得分:1)
看起来您传入的JSON格式不正确。您已将引用定义为String,但将其作为数值传递。我建议将引用更改为Integer,或者在将其作为JSON传递时将其值包装在引号中。