英文:
I am getting a java.util.MissingFormatArgumentException: Format specifier '%s' error in java
问题
我遇到了 java.lang.RuntimeException 异常:
在以下的 Java 代码中出现了 java.util.MissingFormatArgumentException 异常:
以下是我的堆栈跟踪:
org.springframework.beans.factory.BeanCreationException: 在 bean 创建时出现错误 'transformToListClass':初始化方法的调用失败;嵌套异常是 java.lang.RuntimeException: java.util.MissingFormatArgumentException: 格式说明符 '%s'
由于以下原因导致:
java.lang.RuntimeException: java.util.MissingFormatArgumentException: 格式说明符 '%s'
原因是 java.util.MissingFormatArgumentException 异常:
java.util.MissingFormatArgumentException: 格式说明符 '%s'
我正在传递以下 JSON 文件:
{
"EcrionIntegration": {
"HelloWord": "Hello World"
}
}
给以下方法:
@PostConstruct
public ResponseEntity<InputStreamResource> tranform() {
try {
JSONParser parser = new JSONParser();
// 使用 JSONObject 处理简单的 JSON,使用 JSONArray 处理 JSON 数组。
JSONObject data = (JSONObject) parser.parse(new FileReader("C:\\Liver\\code\\HELLO_WORLD.json")); // JSON 文件路径。
String payLoad = data.toString();
ImageDescriptor descriptor = ecrionService.generateImage(payLoad, "HELLO_WORLD");
log.info(String.format("%s generateImage 结果:[%s] ", descriptor.getFileName(), descriptor.getFileUrl()));
System.out.print(descriptor.getFileName() + " " + descriptor.getFileUrl());
String fileName = "ecrionlList.PDF";
log.info(String.format("文件名:%s", fileName));
System.out.print("文件名:" + fileName);
descriptor.setFileName(fileName);
InputStreamResource streamResource = new InputStreamResource(descriptor.getInputStream());
return ResponseEntity.ok()
.contentType(MediaType.parseMediaType(MediaType.APPLICATION_PDF_VALUE))
.body(streamResource);
} catch (Exception ex) {
throw new RuntimeException(ex);
}
}
}
然后将其传递给以下方法并发送到服务器:
public ImageDescriptor generateImage(String payLoad, String templateName) {
try {
ImageDescriptor descriptor = new ImageDescriptor();
String myEcrionUrl = "http://localhost:8013/v1/ecrion";
String ecrionURL = myEcrionUrl.concat(Constant.F_SLASH).concat(templateName);
log.info("负载:" + payLoad);
ResponseEntity<Resource> responseEntity = restTemplate.exchange(
ecrionURL,
HttpMethod.POST,
ncbiService.getStringHttpEntityWithPayload(payLoad),
Resource.class);
log.info(String.format("%s generateImage 结果:[%s] ", descriptor.getFileName(), responseEntity.getBody().getInputStream()));
descriptor.setInputStream(Objects.requireNonNull(responseEntity.getBody()).getInputStream());
convert(responseEntity.getBody().getInputStream(), "sherrr.pdf");
log.info("文件是:" + convert(responseEntity.getBody().getInputStream(), "sherrr.pdf"));
return descriptor;
} catch (IOException e) {
e.printStackTrace();
log.error("生成图像失败:" + e.getMessage());
throw new RuntimeException(e);
}
}
英文:
I am getting a java.lang.RuntimeException:
java.util.MissingFormatArgumentException: Format specifier '%s' error in Java below is my stacktrace:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'transformToListClass': Invocation of init method failed; nested exception is java.lang.RuntimeException: java.util.MissingFormatArgumentException: Format specifier '%s'
Caused by: java.lang.RuntimeException: java.util.MissingFormatArgumentException: Format specifier '%s'
Caused by: java.util.MissingFormatArgumentException: Format specifier '%s'
I am passing the below JSON file:
{
"EcrionIntegration": {
"HelloWord": "Hello World"
}
}
To the following method:
@PostConstruct
public ResponseEntity<InputStreamResource> tranform() {
try {
JSONParser parser = new JSONParser();
//Use JSONObject for simple JSON and JSONArray for array of JSON.
JSONObject data = (JSONObject) parser.parse(new FileReader("C:\\Liver\\code\\HELLO_WORLD.json"));//path to the JSON file.
String payLoad = data.toString();
ImageDescriptor descriptor = ecrionService.generateImage(payLoad, "HELLO_WORLD");
log.info(String.format("%s generateImage Result: [%s] ", descriptor.getFileName(), descriptor.getFileUrl()));
System.out.print(descriptor.getFileName() + " " + descriptor.getFileUrl());
String fileName = "ecrionlList.PDF";
log.info(String.format("file name:", fileName));
System.out.print("file name:" + fileName);
descriptor.setFileName(fileName);
InputStreamResource streamResource = new InputStreamResource(descriptor.getInputStream());
return ResponseEntity.ok()
.contentType(MediaType.parseMediaType(MediaType.APPLICATION_PDF_VALUE))
.body(streamResource);
} catch (Exception ex) {
throw new RuntimeException(ex);
}
}
}
Which is in turn passed to the following method and send to the server:
public ImageDescriptor generateImage(String payLoad, String templateName) {
try {
ImageDescriptor descriptor = new ImageDescriptor();
String myEcrionUrl = "http://localhost:8013/v1/ecrion";
String ecrionURL = myEcrionUrl.concat(Constant.F_SLASH).concat(templateName);
log.info("payload" + payLoad);
ResponseEntity<Resource> responseEntity = restTemplate.exchange(
ecrionURL,
HttpMethod.POST,
ncbiService.getStringHttpEntityWithPayload(payLoad),
Resource.class);
log.info(String.format("%s generateImage Result: [%s] ", responseEntity.getBody().getInputStream()));
descriptor.setInputStream(Objects.requireNonNull(responseEntity.getBody()).getInputStream());
convert(responseEntity.getBody().getInputStream(), "sherrr.pdf");
log.info("file is:"+ convert(responseEntity.getBody().getInputStream(), "sherrr.pdf"));
return descriptor;
} catch (IOException e) {
e.printStackTrace();
log.error(" generate image failed " + e.getMessage());
throw new RuntimeException(e);
}
}
答案1
得分: 2
log.info(String.format("%s 生成图像结果: [%s] ", responseEntity.getBody().getInputStream()));
这个格式有两个 %s,但你只传了一个参数。
英文:
log.info(String.format("%s generateImage Result: [%s] ", responseEntity.getBody().getInputStream()));
This format has two %s, but you only passed one argument.
答案2
得分: 0
如你可以从文档中看到,
String.format接受格式字符串和vararg参数。如果你查看格式化字符串的文档,你可以看到在百分号(%)后面有一系列的参数,可以用来格式化字符串。特别是对于%s
,它的说明如下:
's', 'S' 通用 如果参数arg为null,则结果为“null”。如果arg实现了Formattable接口,则会调用arg.formatTo方法。否则,通过调用arg.toString()来获得结果。
所以你之前遇到的错误是因为你告诉格式化字符串有2个参数将被格式化,但你只传递了其中一个:
log.info(String.format("%s generateImage Result: [%s]", responseEntity.getBody().getInputStream()));
所以你只需移除第一个%s
:
log.info(String.format("generateImage Result: [%s]", responseEntity.getBody().getInputStream()));
英文:
As you can see by documentation
String.format accepts Format string and vararg or parameters. If you look at the formatter string documentation you can see that after %
sign there are a bunch of parameters which can be used to format the string. Particularly for the %s
it's written the following
's', 'S' general If the argument arg is null, then the result is "null". If arg implements Formattable, then arg.formatTo is invoked. Otherwise, the result is obtained by invoking arg.toString().
So the error you were getting was because you were telling formatter string that 2 parameters will be formatted and you passed only one of it
log.info(String.format("%s generateImage Result: [%s] ", responseEntity.getBody().getInputStream()));
So you can just remove the first %s
:
log.info(String.format("generateImage Result: [%s] ", responseEntity.getBody().getInputStream()));
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论