Accent problem while consuming Webservice on Windows Server

Asked

Viewed 1,058 times

4

Correction

I have an accentuation problem when consuming Webservice on Windows Server. I have apache-Tomcat-7.0.63 installed on windows server 2008, and as SGB, Postgres 9.4 and with Java 8. The error happens when I run outside of Netbeans. That is, if run outside in any environment it saves the wrong characters, but if run inside Netbeans (Debugging or Running only) it saves the normal characters.

imagem do erro

web service code

@POST
@Path("cliente/sincronizar")
@Consumes("application/json;charset=utf-8")
public Response createClienteInJSON(String param) {
    Gson gson = new Gson();
    ContaCTR contaCTR = new ContaCTR();

    ParamCliente paramCliente = gson.fromJson(param, ParamCliente.class);

    Conta conta = contaCTR.carregar(paramCliente.getToken());

    if (!paramCliente.getClientes().isEmpty() && conta != null) {
        try {
            WsAuxiliar.sincronizarCliente(paramCliente.getClientes(), conta);

            ClienteCTR clienteCTR = new ClienteCTR();
            List<Cliente> clientes = clienteCTR.listar(conta.getContaid(), "codigointerno");
            WsAuxiliar.sincronizarEmail(paramCliente.getEmails(), conta, clientes);
            WsAuxiliar.sincronizarEndereco(paramCliente.getEnderecos(), conta, clientes);
            WsAuxiliar.sincronizarTelefone(paramCliente.getTelefones(), conta, clientes);
        } catch (Exception e) {
            throw new RuntimeException("Falha ao sincronizar Cliente. Erro: " + e.getMessage());
        }
    }
    return Response.status(201).build();
}

client code consuming web service

    public Retorno sendPostClientes(String token) throws Exception {
    try {
        ParamCliente paramCliente = new ParamCliente();
        paramCliente.carregar();
        paramCliente.setToken(token );

        Gson gson = new Gson();

        String input = gson.toJson(paramCliente);

        URL url = new URL(Configuracao.carregar().getWebserv() + "cliente/sincronizar");
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setDoOutput(true);
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Content-Type", "application/json;charset=utf-8");            

        OutputStream os = conn.getOutputStream();
        os.write(input.getBytes());
        os.flush();

        if (conn.getResponseCode() != HttpURLConnection.HTTP_CREATED) {
            throw new RuntimeException("Failed : HTTP error code : "
                    + conn.getResponseCode());
        }

        BufferedReader br = new BufferedReader(new InputStreamReader(
                (conn.getInputStream())));

        String output;
        System.out.println("Output from Server .... \n");
        while ((output = br.readLine()) != null) {
            System.out.println(output);
        }

        conn.disconnect();
    } catch (Exception e) {
        return Retorno.criarRetorno(Boolean.TRUE, "Erro ao sincronizar clientes!\n" + e.getMessage());
    }
    return Retorno.criarRetorno(Boolean.FALSE, "Clientes Sincronizado com sucesso!");
}

Configuration of the Postgres database

inserir a descrição da imagem aqui

Thank you so much for your help.

  • Someone would have a hint?

  • Have you looked at the postgree settings?

  • I’m sorry, because the error happens when I run outside of Netbeans. That is, if I run out in any environment it goes with the wrong characters, but if I run inside Netbeans (Debugging or just Running) it sends the normal characters. And I still have no solution for this error...

  • 1

    Yes, but your postgree table is UTF-8 ?

  • Yes it this UTF-8, I put up an image with the settings of the bank.

  • 1

    I’m thinking about what it might be.

  • 1

    Try to use ISO-8859-1 and see what

  • 1

    With ISO-8859-1 it worked perfectly outside of the Netbeans IDE but inside (Debugging or simply Running) it stopped working. Crazy thing... Programmer thing...kkkk

  • Strange... Something is going unnoticed, rs

  • Maybe Netbeans is set to UTF-8

  • 1

    I’m going to leave ISO-8859-1 because it’s more important to work on the separate executable than it is in Netbeans. So I’ve partially solved it. Thank you Techies. May God reward you.

  • For nothing, Since you solved partially wait a while and see if another person responds with a better solution, in case it happens do not forget to put as solved your problem, so it can help other people.

  • @Joãopaulo you can post your problem solution as an answer below and mark as accepted. The site works different from forums.

Show 8 more comments

1 answer

2

Try the following:

@Produces("application/json; charset=UTF-8")
public Response createClienteInJSON(String param) {
...
}

I hope it works

  • But he’s not doing it anymore?

  • I’m sorry, because the error happens when I run outside of Netbeans. That is, if I run out in any environment it goes with the wrong characters, but if I run inside Netbeans (Debugging or just Running) it sends the normal characters. And I still have no solution for this error...

  • @Techies by code posted no, which has the charset there is the @Consumes. Probably the error is to produce the same answer. @Joãopaulo managed to solve only by configuring the @Produces?

  • I actually tested it the way it did for me, too, but it made the same mistake. Changing the charset reversed the location of the rsrs error... before it worked inside the netbeans and external no, now it works outside but inside the NB stopped working... But since the external executable is more important, I say I managed to get around the error. Thank you very much @Brunocésar and sorry for the delay in the return, but the holiday was far from civilization..

Browser other questions tagged

You are not signed in. Login or sign up in order to post.