简体   繁体   中英

Jersey - 405 Method Not Allowed

I'm new with Web Services. I have a working endpoint which returns me a JSON with the person data, if the parameters (identifier and password) are informed correctly.

Like this:

http://www.issdigitalthe.com.br/controleacesso/servico/api/login/4/SMT/000000000000/password/false

Where 000000000000 is my identifier, and "password" is my password.

Testing with PostMan (POST), this is my return:

"id": 40001002131,
    "idOriginal": 123131,
    "cpfCnpj": "000000000",
    "nome": "JOHN DOE",
    "idCargo": 131313131,
    "descricaoCargo": "Usuário responsável por analisar e Deferir/Indeferir os Protocolos.",
    "idDepartamento": 4000200000000761,
    "descricaoDepartamento": "Departamento para usuários ",
    "login": "000000000",
    "email": "john@gmail.com",
    "ultimoAcesso": 1537824078846,
    "certificadoDigital": "N",
    "dataCriacao": null,
    "bairro": "Bairro",
    "cep": "64020340",
    "complemento": "Quadra 00 Casa 111",
    "dddCelular": "86 ",
    "dddTelefone": "86 ",
    "celular": "99999999",
    "telefone": "86 ",
    "uf": "PI",
    "municipio": "SÃO PAULO",

I have to consume that service to check if the return is valid or not. Here's my code:

import javax.faces.bean.ManagedBean;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;

@Path("/")
@ManagedBean
public class WebServiceLoginMB {

    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON + MediaType.TEXT_HTML)

    public String getDados() {
        Client c = Client.create();
        WebResource wr = c.resource("//www.issdigitalthe.com.br/controleacesso/servico/api/login/4/SMT/000000000000/password/false);
        return wr.get(String.class); 
      }
}

At first, i was just trying to see the return on my Glassfish console, and then i would check if the return is correct or not. But i'm always getting "returned a response status of 405 Method Not Allowed".

It worked like this:

    public static void ws(String user, String pass) {

        try {

            Client client = Client.create();
            WebResource webResource = client.resource(
"http://www.issdigitalthe.com.br/controleacesso/servico/api/login/4/SMT/"+ user + "/"+ pass + "/false");
            ClientResponse response = webResource.type("application/json").post(ClientResponse.class);

            String output = response.getEntity(String.class);
            Gson gson = new Gson();
            PessoaNota pessoa = gson.fromJson(output, PessoaNota.class);
            System.out.println(pessoa.getNome());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

I know it still got some things to adjust, but the first problem was solved. Thanks to everyone who helped.

You get this error because you are performing HTTP GET call, while in postman you tried HTTP POST. Try to make a post call like this

public String getDados() {
    Client c = Client.create();
    WebResource wr = c.resource("http://www.issdigitalthe.com.br/controleacesso/servico/api/login/4/SMT/000000000000/password/false);
    return wr.post(String.class); 
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM