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

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

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 &#39;%s&#39; error in Java below is my stacktrace:

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;

Caused by: java.lang.RuntimeException: java.util.MissingFormatArgumentException: Format specifier &#39;%s&#39;

Caused by: java.util.MissingFormatArgumentException: Format specifier &#39;%s&#39;

I am passing the below JSON file:

{
&quot;EcrionIntegration&quot;: {
  &quot;HelloWord&quot;: &quot;Hello World&quot;
}
}

To the following method:

@PostConstruct
public ResponseEntity&lt;InputStreamResource&gt; tranform() {
    try {

        JSONParser parser = new JSONParser();
//Use JSONObject for simple JSON and JSONArray for array of JSON.
        JSONObject data = (JSONObject) parser.parse(new FileReader(&quot;C:\\Liver\\code\\HELLO_WORLD.json&quot;));//path to the JSON file.

        String payLoad = data.toString();


        ImageDescriptor descriptor = ecrionService.generateImage(payLoad, &quot;HELLO_WORLD&quot;);

        log.info(String.format(&quot;%s generateImage Result: [%s] &quot;, descriptor.getFileName(), descriptor.getFileUrl()));
        System.out.print(descriptor.getFileName() + &quot; &quot; + descriptor.getFileUrl());

        String fileName = &quot;ecrionlList.PDF&quot;;
        log.info(String.format(&quot;file name:&quot;, fileName));
        System.out.print(&quot;file name:&quot; + 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 = &quot;http://localhost:8013/v1/ecrion&quot;;
      String ecrionURL = myEcrionUrl.concat(Constant.F_SLASH).concat(templateName);



        log.info(&quot;payload&quot; + payLoad);

        ResponseEntity&lt;Resource&gt; responseEntity = restTemplate.exchange(
                ecrionURL,
                HttpMethod.POST,
                ncbiService.getStringHttpEntityWithPayload(payLoad),
                Resource.class);
      log.info(String.format(&quot;%s generateImage Result: [%s] &quot;, responseEntity.getBody().getInputStream()));
        descriptor.setInputStream(Objects.requireNonNull(responseEntity.getBody()).getInputStream());

        convert(responseEntity.getBody().getInputStream(), &quot;sherrr.pdf&quot;);

        log.info(&quot;file is:&quot;+ convert(responseEntity.getBody().getInputStream(), &quot;sherrr.pdf&quot;));

        return descriptor;
    } catch (IOException e) {
        e.printStackTrace();
        log.error(&quot; generate image failed &quot; + e.getMessage());
        throw new RuntimeException(e);
    }




 }

答案1

得分: 2

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

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

英文:
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,它的说明如下:

'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

&#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

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

So you can just remove the first %s :

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:

确定