有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

控制器java。util。MissingFormatArgumentException:格式说明符“%s”

我显然错过了什么,但我不知道
小事情比复杂的事情更让你发疯,这太愚蠢了

这是控制器的代码:

    @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);
}

这是GetClienteNameCognome:

    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()));
    }

}

共 (2) 个答案

  1. # 1 楼答案

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

    应该是

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

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

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

    在最后一个%s之后有一个额外的),所以可能它只是没有正确地读取它?除非这只是复制粘贴代码时的一个错误

    让我们知道这是否有效