I am getting a java.util.MissingFormatArgumentException: Format specifier '%s' error in java

huangapple go评论110阅读模式
英文:

I am getting a java.util.MissingFormatArgumentException: Format specifier '%s' error in java

问题

我遇到了 java.lang.RuntimeException 异常:

  1. 在以下的 Java 代码中出现了 java.util.MissingFormatArgumentException 异常:

以下是我的堆栈跟踪:

  1. org.springframework.beans.factory.BeanCreationException: bean 创建时出现错误 'transformToListClass':初始化方法的调用失败;嵌套异常是 java.lang.RuntimeException: java.util.MissingFormatArgumentException: 格式说明符 '%s'

由于以下原因导致:

  1. java.lang.RuntimeException: java.util.MissingFormatArgumentException: 格式说明符 '%s'

原因是 java.util.MissingFormatArgumentException 异常:

  1. java.util.MissingFormatArgumentException: 格式说明符 '%s'

我正在传递以下 JSON 文件:

  1. {
  2. "EcrionIntegration": {
  3. "HelloWord": "Hello World"
  4. }
  5. }

给以下方法:

  1. @PostConstruct
  2. public ResponseEntity<InputStreamResource> tranform() {
  3. try {
  4. JSONParser parser = new JSONParser();
  5. // 使用 JSONObject 处理简单的 JSON,使用 JSONArray 处理 JSON 数组。
  6. JSONObject data = (JSONObject) parser.parse(new FileReader("C:\\Liver\\code\\HELLO_WORLD.json")); // JSON 文件路径。
  7. String payLoad = data.toString();
  8. ImageDescriptor descriptor = ecrionService.generateImage(payLoad, "HELLO_WORLD");
  9. log.info(String.format("%s generateImage 结果:[%s] ", descriptor.getFileName(), descriptor.getFileUrl()));
  10. System.out.print(descriptor.getFileName() + " " + descriptor.getFileUrl());
  11. String fileName = "ecrionlList.PDF";
  12. log.info(String.format("文件名:%s", fileName));
  13. System.out.print("文件名:" + fileName);
  14. descriptor.setFileName(fileName);
  15. InputStreamResource streamResource = new InputStreamResource(descriptor.getInputStream());
  16. return ResponseEntity.ok()
  17. .contentType(MediaType.parseMediaType(MediaType.APPLICATION_PDF_VALUE))
  18. .body(streamResource);
  19. } catch (Exception ex) {
  20. throw new RuntimeException(ex);
  21. }
  22. }

}

然后将其传递给以下方法并发送到服务器:

  1. public ImageDescriptor generateImage(String payLoad, String templateName) {
  2. try {
  3. ImageDescriptor descriptor = new ImageDescriptor();
  4. String myEcrionUrl = "http://localhost:8013/v1/ecrion";
  5. String ecrionURL = myEcrionUrl.concat(Constant.F_SLASH).concat(templateName);
  6. log.info("负载:" + payLoad);
  7. ResponseEntity<Resource> responseEntity = restTemplate.exchange(
  8. ecrionURL,
  9. HttpMethod.POST,
  10. ncbiService.getStringHttpEntityWithPayload(payLoad),
  11. Resource.class);
  12. log.info(String.format("%s generateImage 结果:[%s] ", descriptor.getFileName(), responseEntity.getBody().getInputStream()));
  13. descriptor.setInputStream(Objects.requireNonNull(responseEntity.getBody()).getInputStream());
  14. convert(responseEntity.getBody().getInputStream(), "sherrr.pdf");
  15. log.info("文件是:" + convert(responseEntity.getBody().getInputStream(), "sherrr.pdf"));
  16. return descriptor;
  17. } catch (IOException e) {
  18. e.printStackTrace();
  19. log.error("生成图像失败:" + e.getMessage());
  20. throw new RuntimeException(e);
  21. }
  22. }
英文:

I am getting a java.lang.RuntimeException:

  1. java.util.MissingFormatArgumentException: Format specifier &#39;%s&#39; error in Java below is my stacktrace:
  2. org.springframework.beans.factory.BeanCreationException: Error creating bean with name &#39;transformToListClass&#39;: Invocation of init method failed; nested exception is java.lang.RuntimeException: java.util.MissingFormatArgumentException: Format specifier &#39;%s&#39;
  3. Caused by: java.lang.RuntimeException: java.util.MissingFormatArgumentException: Format specifier &#39;%s&#39;
  4. Caused by: java.util.MissingFormatArgumentException: Format specifier &#39;%s&#39;

I am passing the below JSON file:

  1. {
  2. &quot;EcrionIntegration&quot;: {
  3. &quot;HelloWord&quot;: &quot;Hello World&quot;
  4. }
  5. }

To the following method:

  1. @PostConstruct
  2. public ResponseEntity&lt;InputStreamResource&gt; tranform() {
  3. try {
  4. JSONParser parser = new JSONParser();
  5. //Use JSONObject for simple JSON and JSONArray for array of JSON.
  6. JSONObject data = (JSONObject) parser.parse(new FileReader(&quot;C:\\Liver\\code\\HELLO_WORLD.json&quot;));//path to the JSON file.
  7. String payLoad = data.toString();
  8. ImageDescriptor descriptor = ecrionService.generateImage(payLoad, &quot;HELLO_WORLD&quot;);
  9. log.info(String.format(&quot;%s generateImage Result: [%s] &quot;, descriptor.getFileName(), descriptor.getFileUrl()));
  10. System.out.print(descriptor.getFileName() + &quot; &quot; + descriptor.getFileUrl());
  11. String fileName = &quot;ecrionlList.PDF&quot;;
  12. log.info(String.format(&quot;file name:&quot;, fileName));
  13. System.out.print(&quot;file name:&quot; + fileName);
  14. descriptor.setFileName(fileName);
  15. InputStreamResource streamResource = new InputStreamResource(descriptor.getInputStream());
  16. return ResponseEntity.ok()
  17. .contentType(MediaType.parseMediaType(MediaType.APPLICATION_PDF_VALUE))
  18. .body(streamResource);
  19. } catch (Exception ex) {
  20. throw new RuntimeException(ex);
  21. }
  22. }

}

Which is in turn passed to the following method and send to the server:

  1. public ImageDescriptor generateImage(String payLoad, String templateName) {
  2. try {
  3. ImageDescriptor descriptor = new ImageDescriptor();
  4. String myEcrionUrl = &quot;http://localhost:8013/v1/ecrion&quot;;
  5. String ecrionURL = myEcrionUrl.concat(Constant.F_SLASH).concat(templateName);
  6. log.info(&quot;payload&quot; + payLoad);
  7. ResponseEntity&lt;Resource&gt; responseEntity = restTemplate.exchange(
  8. ecrionURL,
  9. HttpMethod.POST,
  10. ncbiService.getStringHttpEntityWithPayload(payLoad),
  11. Resource.class);
  12. log.info(String.format(&quot;%s generateImage Result: [%s] &quot;, responseEntity.getBody().getInputStream()));
  13. descriptor.setInputStream(Objects.requireNonNull(responseEntity.getBody()).getInputStream());
  14. convert(responseEntity.getBody().getInputStream(), &quot;sherrr.pdf&quot;);
  15. log.info(&quot;file is:&quot;+ convert(responseEntity.getBody().getInputStream(), &quot;sherrr.pdf&quot;));
  16. return descriptor;
  17. } catch (IOException e) {
  18. e.printStackTrace();
  19. log.error(&quot; generate image failed &quot; + e.getMessage());
  20. throw new RuntimeException(e);
  21. }
  22. }

答案1

得分: 2

  1. log.info(String.format("%s 生成图像结果: [%s] ", responseEntity.getBody().getInputStream()));

这个格式有两个 %s,但你只传了一个参数。

英文:
  1. log.info(String.format(&quot;%s generateImage Result: [%s] &quot;, responseEntity.getBody().getInputStream()));

This format has two %s, but you only passed one argument.

答案2

得分: 0

如你可以从文档中看到,
String.format接受格式字符串和vararg参数。如果你查看格式化字符串的文档,你可以看到在百分号(%)后面有一系列的参数,可以用来格式化字符串。特别是对于%s,它的说明如下:

  1. 's', 'S' 通用 如果参数argnull,则结果为“null”。如果arg实现了Formattable接口,则会调用arg.formatTo方法。否则,通过调用arg.toString()来获得结果。

所以你之前遇到的错误是因为你告诉格式化字符串有2个参数将被格式化,但你只传递了其中一个:

  1. log.info(String.format("%s generateImage Result: [%s]", responseEntity.getBody().getInputStream()));

所以你只需移除第一个%s

  1. 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

  1. &#39;s&#39;, &#39;S&#39; general If the argument arg is null, then the result is &quot;null&quot;. 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

  1. log.info(String.format(&quot;%s generateImage Result: [%s] &quot;, responseEntity.getBody().getInputStream()));

So you can just remove the first %s :

  1. log.info(String.format(&quot;generateImage Result: [%s] &quot;, responseEntity.getBody().getInputStream()));

huangapple
  • 本文由 发表于 2020年8月21日 12:49:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/63516697.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定