Java Jersey PUT Method and working Client

25,709

Solution 1

How do I access this String in my PUT-method?

You can simply code the method to take an argument of type String and Jersey will map this from the incoming XML request, so:

@PUT
@Consumes(MediaType.TEXT_XML)
@Path("/result")
public void putIntoDAO(String xml) {
    // ...
}

Should work, with the String containing the full request body.

How do I send an XML-String to my WebService?

This depends on what you're using to send the request to the service, which could be anything which communicates over HTTP. I'll assume you're using Java and sticking with Jersey, so one option is you can use the Jersey Client in the following way:

Client client = Client.create();
WebResource webResource = client.resource("http://localhost:8080/input/result");
String input = "<xml></xml>";
ClientResponse response = webResource
    .type("application/xml")
    .put(ClientResponse.class, input);

See the Jersey Client documentation for more.

Solution 2

The answer Ross Turner posted is completely correct and working. Here is an option using Apache HttpComponents.

import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;

public class Runner {

    public static void main(String[] args) {
        try {

            HttpClient httpClient = new DefaultHttpClient();
            HttpPut putRequest = new HttpPut("http://localhost:8080/HelloFromJersey/input/result");

            StringEntity input = new StringEntity("Hello, this is a message from your put client!");
            input.setContentType("text/xml");
            putRequest.setEntity(input);

            httpClient.execute(putRequest);
            httpClient.getConnectionManager().shutdown();


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

The server prints:

Hello, this is a message from your put client!
Share:
25,709
silenum
Author by

silenum

Updated on May 26, 2020

Comments

  • silenum
    silenum about 4 years

    I am developing an Dynamic Web Application with Eclipse. I have e working MySQL-Database which is connected over a class called 'Data Access Object' (=DAO) that works with JDBC. I want to create entries into this database. The functions are ready. With ready I mean tested and OK. On the same application I implemented Java Jersey's RESTful WebService. It is working well, I can call the service and it returns my desired information. But now to my question:

    How can I send a String containing XML? The String has to be parsed in the WebMethod to build and execute the query.

    My WebService looks as follows:

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    
    import javax.ws.rs.Consumes;
    import javax.ws.rs.PUT;
    import javax.ws.rs.Path;
    import javax.ws.rs.core.MediaType;
    
    @Path("/input")
    public class Input {
        //DAO instance to have connection to the database.
        //Not used yet.
        //private DAO dao = new DAO();
    
        @PUT
        @Consumes(MediaType.TEXT_XML)
        @Path("/result")
        public void putIntoDAO(InputStream xml) {
            String line = "";
            StringBuilder sb = new  StringBuilder();
            try {
                BufferedReader br = new BufferedReader(new InputStreamReader(xml));
                while ((line = br.readLine()) != null) {
                    sb.append(line);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            System.out.println(sb.toString());
        }
    }
    

    As you see I tried to print the incoming Stream to the console. I repeat the most important things:

    • I know how to parse XML.
    • I know my DAO works properly.
    • I know my WebService works as well.

    What I would like to know:

    • How do I send an XML-String to my WebService?
    • How do I access this String in my PUT-method?

    Thank you for your attention and try to help me. I appreciate even every try to.

    Kind regards

    L.

  • silenum
    silenum over 9 years
    Thank you @Ross Turner! It is finally working! I also tried out the Apache HttpComponents which are working as well. I will post the example below your answer.