java.util.MissingFormatArgumentException: 格式说明符“%s”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38096651/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 03:06:40  来源:igfitidea点击:

java.util.MissingFormatArgumentException: Format specifier '%s'

javacontrollerresponseresttemplate

提问by Leon

I'm obviously missing something but I don't know what...
It's stupid how much little things make you go crazy more than complicate ones...

This is the controller's code:

我显然遗漏了一些东西,但我不知道是什么......
愚蠢的小事情比复杂的事情更让你发疯......

这是控制器的代码:

    @RequestMapping(value = "/getClienteNomeCognome", method = RequestMethod.GET)
public ResponseEntity<List<Object>> getClienteNomeCognome(@RequestParam("nomeCliente") String nomeCliente,
        @RequestParam("cognomeCliente") String cognomeCliente) {
    List<Object> listaRisultati = new ArrayList<Object>();
    try {
        listaRisultati = serviziDocumentaleService.getClienteNomeCognome(nomeCliente, cognomeCliente);
    } catch (Exception e) {
        LOGGER.warn(String.format("Errore inatteso sulla chiamata del servizio: [%s]", e.toString()));
    }
    LOGGER.info(String.format("Avvio ricerca cliente con nome: %s, cognome: %s)", nomeCliente, cognomeCliente));
    return new ResponseEntity<List<Object>>(listaRisultati, HttpStatus.OK);
}

And this is getClienteNomeCognome:

这是 getClienteNomeCognome:

    public List<Object> getClienteNomeCognome(String nome, String cognome) throws Exception {
    try {
        final RestTemplate restTemplate = new RestTemplate();
        final String url = "somelink?cognome=%25"+cognome+"%25&nome=%25"+nome+"%25";
        final ResponseEntity<List> response = (ResponseEntity<List>) restTemplate.getForObject(url, List.class);
        if (response.getBody() != null && response.getBody().toString().contains("<error>")) {
            throw new Exception(String.format(
                    "La risposta del servizio contiene degli errori: %s",
                    response.getBody()));
        } else {
            LOGGER.debug("Fine chiamata al servizio di ricerca cliente");
            return response.getBody();
        }
    } catch (HttpClientErrorException hcee) {
        throw new Exception(String.format(
                "Errore durante la chiamata. Error: %s",
                hcee.getMessage()));
    } catch (Exception e) {
        throw new Exception(String.format(
                "Errore generico durante la chiamata al servizio. Error: %s"
                        + e.getMessage()));
    }

}

回答by McDowell

    throw new Exception(String.format(
            "Errore generico durante la chiamata al servizio. Error: %s"
                    + e.getMessage()));

should be

应该

    throw new Exception(String.format(
            "Errore generico durante la chiamata al servizio. Error: %s",
                    e.getMessage()));

回答by DGonz

I can't be 100% sure of the issue, since its missing code so I can emulate it from here. But it looks like:

我不能 100% 确定这个问题,因为它缺少代码,所以我可以从这里模拟它。但它看起来像:

LOGGER.info(String.format("Avvio ricerca cliente con nome: %s, cognome: %s)", nomeCliente, cognomeCliente));

has an extra )after the last %s, so maybe its just not reading it correctly? Unless that's just a mistake when copy-pasting the code here.

)在最后一个之后有一个额外的%s,所以也许它只是没有正确阅读它?除非在此处复制粘贴代码时这只是一个错误。

Let us know if that works.

让我们知道这是否有效。