0

I was trying to build a RESTful web service using Jersey.

In my server side code, there is a path with name "domain" which I use to display content. The content of the page the "domain" refers to is accessible only correct username and password are input.

@POST
@Produces(MediaType.APPLICATION_JSON)
@Path("domain")
public ArrayList<String> domainList(@Context HttpServletRequest req) throws Exception{
    Environments environments = new DefaultConfigurationBuilder().build();
    final ALMProfile profile = new ALMProfile();
    profile.setUrl(environments.getAutomation().getAlmProfile().getUrl());
    profile.setUsername((String) req.getSession().getAttribute("username")); 
        //Set username from input, HTML form
    profile.setPassword((String) req.getSession().getAttribute("password"));
        //Set password from input, HTML form
    try (ALMConnection connection = new ALMConnection(profile);) {
        if (connection.getOtaConnector().connected()) {
            Multimap<String, String> domain = connection.getDomains();
            ArrayList<String> domain_names = new ArrayList<String>();
            for(String key : domain.keys()){
                if(domain_names.contains(key)) domain_names.add(key);
            }
            return domain_names; //return the content
        }
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
    return null;
}

When I attempted to test if correct content was returned, I got an error (status=405, reason=Method Not Allowed). Below is my client side test.

public static void main(String[] args){
    Environments environments = new DefaultConfigurationBuilder().build();
    final ALMProfile profile = new ALMProfile();
    profile.setUrl(environments.getAutomation().getAlmProfile().getUrl());
    profile.setUsername("username"); //Creating a profile with username and password
    profile.setPassword("password");
    ClientConfig config = new ClientConfig();
    Client client = ClientBuilder.newClient(config);
    WebTarget target = client.target(getBaseURI());
    String response = target.path("domain").request().accept
        (MediaType.APPLICATION_JSON).get(Response.class).toString(); 
        //Above is the GET method I see from an example, 
        //probably is the reason why 405 error comes from.
    System.out.println(response);

}

private static URI getBaseURI() {
    return UriBuilder.fromUri("http://localhost:8080/qa-automation-console").build();
}

The servlet configuration is good. We have other paths succesfully running. I suspect the reason might come from I used a GET method to do the job that is supposed to be POST. But I am not familiar to Jersey methods I can use.

Does anyone know any methods that I can use to test the functionality?

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
jsh6303
  • 2,010
  • 3
  • 24
  • 50
  • Please read through [the client documentation](https://jersey.java.net/documentation/latest/client.html) of the Jersey documentation. The comments in your server-side code state that the username and password should be obtained by a form but instead you retrieve them from a session. While RESTful services provide access to session through the HttpRequest object, support of sessions through JAX-RS is by intent not directly supported - for some good reasons like scalability and security. – Roman Vottner Jul 27 '15 at 18:24

1 Answers1

1

See 405 Status Code

405 Method Not Allowed

The method specified in the Request-Line is not allowed for the resource identified by the Request-URI. The response MUST include an Allow header containing a list of valid methods for the requested resource.

Your endpoint is for a @POST request. In your client you are trying to get().

See the Client API documentation for information on how to make a POST request. If it is supposed to be a GET request, then simply change the method annotation to @GET.

Also note, for your @POST resource methods, you should always put a @Consumes annotation with the media types the method supports. If the client send a media type not supported, then they will get a 415 not supported as expected. I would have posted an example of the client post, but I have no idea what type are you are expecting because of the missing annotation, also you don't even have a post object as a method parameter so I am not even sure if your method is really even supposed to be for POST.

See Also:

Community
  • 1
  • 1
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720