Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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:

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

}
like image 992
Leon Avatar asked Jun 29 '16 10:06

Leon


People also ask

What is D and S in Java?

%d means number. %0nd means zero-padded number with a length. You build n by subtraction in your example. %s is a string. Your format string ends up being this: "%03d%s", 0, "Apple"

Why is %d used in Java?

The %d specifier is used for integer types, while %f is only used with a Java float or double.


2 Answers

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()));
like image 123
McDowell Avatar answered Nov 17 '22 22:11

McDowell


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

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.

Let us know if that works.

like image 1
DGonz Avatar answered Nov 17 '22 21:11

DGonz