Haris
Haris

Reputation: 4230

HTTP Status 405 - Method Not Allowed (jax-rs service)

Jax-rs service return HTTP Status 405 - Method Not Allowed.

Service:

@GET
@Consumes(MediaType.TEXT_HTML)
@Produces(MediaType.APPLICATION_JSON)
@Path("login")
public User Login(@QueryParam("u") String username, @QueryParam("p") String password) {
   return UserDAO.getInstance().getLogin(username,password)
}

Android:

 public static Boolean Login(User user) {
     String url = "http://myserver.com/AndroidServis/rest/login?u={u}&p={p}";
     RestTemplate restTemplate = new RestTemplate();

     HttpHeaders headers = new HttpHeaders();
     headers.setContentType(MediaType.APPLICATION_JSON);
     
     HashMap<String, String> params = new HashMap<String, String > ();
     params.put("u", user.getUsername().toString());
     params.put("p", user.getPassword().toString());

     HttpEntity entity = new HttpEntity(headers);

     restTemplate.getMessageConverters().add(new GsonHttpMessageConverter());
     restTemplate.getMessageConverters().add(new StringHttpMessageConverter());
     HttpEntity < Korisnici > response = restTemplate.exchange(url, HttpMethod.GET, entity,User.class, params);

 }

Upvotes: 4

Views: 5606

Answers (1)

Alb
Alb

Reputation: 3681

It doesn't make sense for the server to have a @Consumes annotation on the @GET method, as this is typically only used for PUT or POST requests where the client is sending some content to the server. Can you remove this?

Then also remove this from the client code.

headers.setContentType(MediaType.APPLICATION_JSON);

and you may need to uncomment the line you have commented out:

headers.set("Accept", "application/json");

This tells the server what content type is expected in the response so must match what the @Produces of the service.

Upvotes: 2

Related Questions