简体   繁体   中英

Why does Jersey GET request client return 404?

I have a simple WAR application deployed on Glassfish server which has the following resource:

@Path("/oauth")
public class Oauth {

    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String getValidateCallSignature(@QueryParam("oauth_consumer_key") String consumerKey,
                                            @QueryParam("oauth_nonce") String nonce, 
                                            @QueryParam("oauth_signature_method") String signatureMethod,
                                            @QueryParam("oauth_timestamp") long timeStamp, 
                                            @QueryParam("oauth_version") long version, 
                                            @QueryParam("oauth_signature") String signature) {

        System.out.println("### oauth/ called");

        return consumerKey + " --" + nonce + " --" + signatureMethod + " --" + timeStamp + " --" + version + " --" + signature;
    }
}

This app is at localhost:8080/EloquaTest/api/

Where the RESTful services are started by using javax.ws.rs.core.Application instead of the deployment descriptor.

@ApplicationPath("/api")
public class AppStarter extends Application {}

After running the application, I tested it by typing the following URL in the browser: http://localhost:8080/EloquaTest/api/oauth?oauth_nonce=5564316845&oauth_signature=HMAC-SHA1

As expected the result on the browser was:

null --5564316845 --null --0 --0 --HMAC-SHA1

So far so good, now; I created a separate WAR application that uses JSF to simulate the client that calls the WebService mentioned above, where I have a simple button in a facelet which calls a backing bean which in turn calls the WebService, here's the backingbean:

@Model
public class EloquaClientBacking {

    private static final String ELOQUA_TEST_SERVER_URL = "http://esteban.mora.com:8080/EloquaTest/api/oauth";

    private String consumerKey = "09fb64e1-8b5d-406c-b4d5-adf9d318a1d2";
    private String nonce = "9519484";
    private String signatureMethod = "HMAC-SHA1";
    private long timestamp = 1410986606;
    private float version = 1.0f;
    private String signature = "AZbD26DeXrEV6iNLqBAxSXwWURg=";

    public void personifyEloqua() throws URISyntaxException {

        try {
            Client client = ClientBuilder.newClient();

            WebTarget target = client.target(ELOQUA_TEST_SERVER_URL);

            Response oauthResponse = target.queryParam("oauth_consumer_key", consumerKey)
                    .queryParam("oauth_nonce", nonce)
                    .queryParam("oauth_signature_method", signatureMethod)
                    .queryParam("oauth_timestamp", timestamp)
                    .queryParam("oauth_version", version)
                    .queryParam("oauth_signature", signature)
                    .request()
                    .get();

            System.out.println("##Calling: " + target.getUri());

            if (oauthResponse.getStatus() == 200) {
                String entity = oauthResponse.readEntity(String.class);

                System.out.println("######### " + entity);

                FacesContext.getCurrentInstance().addMessage("msg", new FacesMessage("### Entity: " + entity));
            } else {
                FacesContext.getCurrentInstance().addMessage("msg", new FacesMessage(oauthResponse.getStatusInfo().toString() + " - status-code: " + oauthResponse.getStatus()));
            }

            oauthResponse.close();
            client.close();

        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}

This method personifyEloqua() will create a client and perform a GET request to the client, the problem is that I keep getting a 404 not found as a response and I can't seem to understand why.

I've already tried chaining the configuration:

client.target(URL).queryParam(etc,etc).queryParam(etc,etc).request().get();

and

client.target(BASE_URL).path("oauth").queryParam()....

Nothing works, anyone mind pointing out what am I doing wrong ?

Thanks!

It turns out, that I was making a dumb mistake with the data types:

The WebService was receiving long oauth_version but I was sending float , after I changed the service to @QueryParam("oauth_version") float version it all worked as expected!

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