0

I am programming a jax-rs webservice which I want to contact from a java-program. I defined a @POST method which receives a String-array as input and is supposed to return a boolean value.

But really, how can i access this return value in java? I've been searching the web for several hours now, everybody writes example methods which return Strings or something else, but nobody shows how to access the returned value from another java program.

Here is the code from the program that contacts the @POST method:

ObjectOutputStream oos = null;
        String[] login = {"XXXXXX","XXXXXXX"};
        try {
            login[1] = PasswordGenerator.hashPassword(login[1]);
        } catch (NoSuchAlgorithmException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        URL url = new URL("XXXXX/XXXXXXX/XXXXXX/users/login/1");
        try {
            // creates a HTTP connection
            HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
            httpConn.setUseCaches(false);
            httpConn.setDoOutput(true);
            httpConn.setRequestMethod("POST");
            httpConn.setRequestProperty("Boolean", "application/xml");
            OutputStream outputStream = httpConn.getOutputStream();
            oos = new ObjectOutputStream(outputStream);
            outputStream.close();

            System.out.println();
            } finally {
            if (oos != null) {
                oos.close();
            }
        }
    }

What I want to know is: What happens after I closed my outputStream? I mean, i started the POST method, but it is supposed to return a boolean value. Where is this value? How can I access it???

PKlumpp
  • 4,913
  • 8
  • 36
  • 64

1 Answers1

4

JAX-RS 2.0 has a Client API that provides you with a fluent API to retrieve the content of the response:

Client client = ClientBuilder.newClient();
    Boolean result = client.target("http://localhost:8080/xxx/")
            .path("user/login/1")
            .request(MediaType.TEXT_PLAIN_TYPE)
            .post(Entity.entity(login, MediaType.APPLICATION_XML) , Boolean.class);

But unfortunately, you'll need a custom MessageBodyWriter to convert the String[] into an XML document. Maybe you should change your server-side method (and client) to manipulate a DTO - a POJO with 2 fields, username and password - and annotated with @XmlRootElement ?

something like that:

(client-side)

Credentials credentials = new 
credentials.setUsername("foo");
credentials.setUsername("hashedPwd");
Client client = ClientBuilder.newClient();
Boolean result = client.target("http://xxxxx")
            .path("/user/login/1")
            .request(MediaType.TEXT_PLAIN_TYPE)
            .post(Entity.entity(credentials, MediaType.APPLICATION_XML) , Boolean.class);
    System.out.println("Result: " + result);

(server-side)

@Path("/login/{id}")
@POST
public Boolean test(@PathParam("id") String login, Credentials credentials) {
...
}
Xavier Coulon
  • 1,580
  • 10
  • 15
  • .post(List.class) gives me: The method post(Entity>) in the type SyncInvoker is not applicable for the arguments (Class) – PKlumpp Apr 02 '14 at 14:18
  • thx for this. I ended up working with an InputStream which I feel is much more comfortable ;) – PKlumpp Apr 02 '14 at 15:48
  • You're welcome. InputStream is a bit "lower" level, but if you feel more comfortable this way, then it's good, too ;-) – Xavier Coulon Apr 02 '14 at 17:18