public class BillQueryResponse { @JacksonXmlProperty(localName = "InvtHead") private InvtHead invtHead; @JacksonXmlProperty(localName = "InvtListDetail") private InvtListDetail invtListDetail; @JacksonXmlProperty(localName = "InOutList") private InOutList inOutList; } org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot construct instance of com.emyiw.invsystem.entity.vo.BillQueryResponse (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value ('0'); nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of com.emyiw.invsystem.entity.vo.BillQueryResponse (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value ('0') at [Source: (PushbackInputStream); line: 43, column: 18] (through reference chain: com.emyiw.invsystem.entity.vo.Results["Data"]->java.util.ArrayList[1])解决以上报错信息
这个错误信息表明在使用JSON反序列化时,无法构造一个类型为com.emyiw.invsystem.entity.vo.BillQueryResponse
的实例,因为该类中没有一个接受String类型参数的构造函数或工厂方法来从值“0”进行反序列化。但是,从你提供的代码来看,BillQueryResponse
类似乎是一个XML序列化的类,并使用了@JacksonXmlProperty
注解来指定XML元素名称。因此,你需要使用XML反序列化器来反序列化该对象,而不是使用JSON反序列化器。
你可以在com.fasterxml.jackson.dataformat.xml.XmlMapper
类中使用readValue
方法来反序列化XML字符串。例如:
XmlMapper xmlMapper = new XmlMapper();
BillQueryResponse response = xmlMapper.readValue(xmlString, BillQueryResponse.class);
这样就可以正确地反序列化BillQueryResponse
对象了。
@JacksonXmlRootElement(localName = "BillHead") public class BillHead { //企业内部编号 @JacksonXmlProperty(localName = "CopListNo") private String CopListNo; //企业编号 @JacksonXmlProperty(localName = "CopNo") private String CopNo; } 获得Http客户端(可以理解为:你得先有一个浏览器;注意:实际上HttpClient与浏览器是不一样的) CloseableHttpClient httpClient = HttpClientBuilder.create().build(); // 创建Post请求 HttpPost httpPost = new HttpPost(WcsHttpUtil.httpUtil.agvs); // 设置ContentType(注:如果只是传普通参数的话,ContentType不一定非要用application/json) httpPost.setHeader("Content-Type", "application/json;charset=utf8"); // 响应模型 CloseableHttpResponse response = null; String jsonString = JSON.toJSONString(object); StringEntity entity = new StringEntity(jsonString, "UTF-8"); // post请求是将参数放在请求体里面传过去的;这里将entity放入post请求体中 httpPost.setEntity(entity); // 由客户端执行(发送)Post请求 response = httpClient.execute(httpPost); // 从响应模型中获取响应实体 HttpEntity responseEntity = response.getEntity(); 使用BillHead接收
可以使用以下代码来接收响应:
CloseableHttpResponse response = null;
try {
response = httpClient.execute(httpPost);
HttpEntity responseEntity = response.getEntity();
if (responseEntity != null) {
String responseString = EntityUtils.toString(responseEntity, "UTF-8");
BillHead billHead = JSON.parseObject(responseString, BillHead.class);
// 处理返回的BillHead对象
}
} finally {
if (response != null) {
response.close();
}
}
其中,responseString
是从响应实体中获取到的字符串,然后使用 JSON.parseObject()
方法将其转换为 BillHead
对象。最后你可以在使用 billHead
对象进行处理。
@Mojo(name="bootJar") public class BootJarMojo extends AbstractMojo { // 可自由获取maven内置变量 @Parameter(defaultValue = "${settings.localRepository}",required = true) private String localRepository; @Parameter(property = "main.class",required = true) private String mainClass; @Componentprotected MavenProject project; public void execute() throws MojoFailureException { getLog().info("project localRepository is " + localRepository); File baseDir = project.getBasedir(); getLog().info("project base dir is " + baseDir); String artifactId = project.getArtifactId(); String version = project.getVersion(); File targetDirectory = new File(baseDir, "target"); File classesDirectory = new File(targetDirectory, "classes"); getLog().info("project classes dir is " +classesDirectory.getAbsolutePath()); List<File> dependencyFiles = project.getDependencyArtifacts() .stream() .map(Artifact::getFile) .filter(Objects::nonNull) .collect(Collectors.toList()); ... } }
这段代码是一个名为BootJarMojo
的Maven插件,它继承自AbstractMojo
类并实现了execute()
方法。这个插件的作用是用于构建Spring Boot项目的可执行jar文件。
该插件包含以下几个成员变量和注解:
localRepository
:通过@Parameter
注解,注入了Maven内置变量${settings.localRepository}
,表示本地仓库的路径。mainClass
:通过@Parameter
注解,注入了一个用户自定义的变量main.class
,表示项目的主类名。project
:通过@Component
注解,注入了MavenProject
对象,表示当前的Maven项目。
在execute()
方法中,插件会执行以下操作:
- 打印出本地仓库路径和项目基础目录。
- 获取项目的ArtifactId和Version。
- 创建目标目录(
${basedir}/target
)和类文件目录(${basedir}/target/classes
)。 - 获取项目依赖的所有jar文件的路径,并将其存储在一个列表中。
根据代码中省略的部分,您可以继续编写插件的逻辑,例如运行Spring Boot的打包命令或其他构建操作。
请注意,该代码是一个基本的示例,您可能需要根据您的具体需求进行修改和扩展。如果您有任何其他问题,请随时提问。
相关推荐
















