Solucionado (ver solução)
Solucionado
(ver solução)
2
respostas

org.springframework.http.converter.HttpMessageNotReadableException

Estou com uma duvida/problema a respeito de envio de caracteres especiais via JSON.

{
    "city":"São Paulo",
    "state":"SP"
}

Quanto tento passar este JSON com o caractere especial "ã", sempre recebo um seguinte erro:

{
   "timestamp": 1503406065217,
   "status": 400,
   "error": "Bad Request",
   "exception": "org.springframework.http.converter.HttpMessageNotReadableException",
   "message": "Could not read JSON document: Invalid UTF-8 middle byte 0x6f\n at [Source: java.io.PushbackInputStream@31711f07; line: 2, column: 13]; nested exception is com.fasterxml.jackson.core.JsonParseException: Invalid UTF-8 middle byte 0x6f\n at [Source: java.io.PushbackInputStream@31711f07; line: 2, column: 13]",
   "path": "/api/user/suggestion"
}

Meu controller:

@RestController
@RequestMapping("/api")
public class AuthController {

    @RequestMapping(value = "/auth", method = RequestMethod.POST)
    public Map<String, Object> auth(@RequestBody LoginRequest request) throws UnsupportedEncodingException {
        Map<String, Object> hashMap = new HashMap<>();
        hashMap.put("email", request.getEmail());
        hashMap.put("password", request.getPassword());
        return hashMap;
    }
}

LoginRequest:

public class LoginRequest {
    private String email;
    private String password;

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

Eu ja pesquisei no stackoverflow e tentei diversas maneiras diferente resolver esse problema e não consegui...

Sera que alguem pode me dar um luz em relação a isso ?

2 respostas

Console:

2017-08-22 10:06:50.546  WARN 9664 --- [nio-8080-exec-2] .w.s.m.s.DefaultHandlerExceptionResolver : Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException: Could not read JSON document: Invalid UTF-8 middle byte 0x6f
 at [Source: java.io.PushbackInputStream@556a7f72; line: 2, column: 13]; nested exception is com.fasterxml.jackson.core.JsonParseException: Invalid UTF-8 middle byte 0x6f
 at [Source: java.io.PushbackInputStream@556a7f72; line: 2, column: 13]
2017-08-22 10:06:50.546  WARN 9664 --- [nio-8080-exec-2] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved exception caused by Handler execution: org.springframework.http.converter.HttpMessageNotReadableException: Could not read JSON document: Invalid UTF-8 middle byte 0x6f
 at [Source: java.io.PushbackInputStream@556a7f72; line: 2, column: 13]; nested exception is com.fasterxml.jackson.core.JsonParseException: Invalid UTF-8 middle byte 0x6f
 at [Source: java.io.PushbackInputStream@556a7f72; line: 2, column: 13]
solução!

Oi Diego, problema de encode sempre é um saco e a solução nunca parece ser clara :(. Já tentou gerar esse json que quer passar com utf8? Tenta fazer esse teste.