I’m new to the java rest CXF client. I will make various requests to a remote server, but first I need to create a Ticket Granting Ticket (TGT). I looked through various sources but I could not find a solution. The server requests that I will create a TGT are as follows:
- Content-Type: text as parameter, application / x-www-form-urlencoded as value
- username
- password
I create TGT when I make this request with the example URL like below using Postman. (URL is example). But in the code below, I’m sending the request, but the response is null. Could you help me with the solution?
The example URL that I make a request with POST method using Postman: https://test.service.com/v1/tickets?format=text&username=user&password=pass
List<Object> providers = new ArrayList<Object>(); providers.add(new JacksonJsonProvider()); WebClient client = WebClient.create("https://test.service.com/v1/tickets?format=text&username=user&password=pass", providers); Response response = client.getResponse();
Advertisement
Answer
You need to do a POST, yet you did not specify what your payload looks like?
Your RequestDTO and ResponseDTO have to have getters/setters.
An example of using JAX-RS 2.0 Client.
Client client = ClientBuilder.newBuilder().register(new JacksonJsonProvider()).build(); WebTarget target = client.target("https://test.service.com/v1/tickets"); target.queryParam("format", "text"); target.queryParam("username", "username"); target.queryParam("password", "password"); Response response = target.request().accept(MediaType.APPLICATION_FORM_URLENCODED).post(Entity.entity(yourPostDTO, MediaType.APPLICATION_JSON)); YourResponseDTO responseDTO = response.readEntity(YourResponseDTO.class); int status = response.getStatus();
Also something else that can help is if you copy the POST request from POSTMAN as cURL request. It might help to see the differences between your request and POSTMAN. Perhaps extra/different headers are added by postman?
Documentation: https://cxf.apache.org/docs/jax-rs-client-api.html#JAX-RSClientAPI-JAX-RS2.0andCXFspecificAPI
Similar Stackoverflow: Is there a way to configure the ClientBuilder POST request that would enable it to receive both a return code AND a JSON object?