0

In my spring application i need to consume third party web service by using this link: How to consume third party WSDL services in Spring MVC. Now i have a scenario to call a web service where I do not send request in object. Which is a get method. So i have to add parameters to the URL. How can i do this?

This is my sample URL:

https://sriharicorp.com/sampleApplication/CoreIssue.aspx?user=srihari&password=srihari36&Application=appscale4631&serviceName=svc&dbbSystemExtLogin=1&accountNumber=125684364836
Community
  • 1
  • 1
srihari
  • 397
  • 2
  • 7
  • 18

5 Answers5

2

Since you're familiar with Spring you could try using the Spring class - RestTemplate

See example here - http://www.springbyexample.org/examples/contact-rest-services-client.html OR here - http://www.informit.com/guides/content.aspx?g=java&seqNum=546

ab2000
  • 364
  • 2
  • 8
  • I'm already consuming soap services. Is there any way other than rest template? – srihari Dec 17 '14 at 09:18
  • 1
    RestTemplate is not for SOAP services. It's the easy way to call HTTP services such as the one you are asking about. – Steve Dec 17 '14 at 14:06
1

Another Alternative to RestTemplate is HttpClient

     HttpClient httpClient = new HttpClient()
    GetMethod get = new GetMethod(adviceGetURL);
    get.addRequestHeader("Content-Type", "application/json");
    try {
        httpClient.executeMethod(get);

            assertEquals(HttpStatus.SC_OK, get.getStatusCode());

    String response = get.getResponseBodyAsString();
    } catch (HttpException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

how to unmarshal an XML to Object

         package com.mkyong.core;

  import java.io.File;
  import javax.xml.bind.JAXBContext;
  import javax.xml.bind.JAXBException;
  import javax.xml.bind.Unmarshaller;

 public class JAXBExample {
public static void main(String[] args) {

 try {

    File file = new File("C:\\file.xml");
    JAXBContext jaxbContext = JAXBContext.newInstance(Customer.class);

    Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
    Customer customer = (Customer) jaxbUnmarshaller.unmarshal(file);
    System.out.println(customer);

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

}

}

paul
  • 12,873
  • 23
  • 91
  • 153
  • How to get the xml response into an object? – srihari Dec 17 '14 at 09:17
  • That´s another question that is out of the scope of your first one. Check the JAXB and theirs javax.xml.bind.Marshaller and javax.xml.bind.Unmarshaller. Regarding the other question, if you consider that the answer was valid add a vote and mark as finish please – paul Dec 17 '14 at 09:26
  • K. But i want to get the response. Then only i can vote this. – srihari Dec 17 '14 at 09:36
  • Then I think is better if you create another ticket with the question how to marhsall an xml into an object in Java. How to consume a webService it´s already answered. Anyway I update my answer. – paul Dec 17 '14 at 09:40
  • Thank you.. I got the response.. – srihari Dec 17 '14 at 09:49
1

To consume a SOAP based web service it could be helpful to use Spring Web Services project.

First of all you need to generate domain objects (classes) from the service WSDL.

Then you need to use two main framework classes WebServiceGatewaySupport and WebServiceTemplate.

The first is an abstract class you need to extend to implement your custom service client.

The second class is a convenient template to access service data (it is structured like others templates in Spring framework; i.e. JdbcTempate).

Take a look to this tutorial.

davioooh
  • 23,742
  • 39
  • 159
  • 250
  • Thank you for your post. I'm already consuming SOAP based web services in my project using "WebServiceTemplate". No i have to call a service which contains parameters in the url. How can i achieve this? – srihari Dec 17 '14 at 09:23
1

This is the exact answer that i'm looking for

public CardDetailsResponse getCardDetails(User userDetails,String destination) {
        CardDetailsResponse cardDetailsResponse=new CardDetailsResponse();
     try    {

        HttpClient httpClient = new HttpClient();
        GetMethod get = new GetMethod("https://sriharicorp.com/sampleApplications/CoreIssue.aspx?serviceName=svc&loginStatus=1&accountNumber=32146546454");
        get.addRequestHeader("Content-Type", "application/json");
        try {
            httpClient.executeMethod(get);
            String response=get.getResponseBodyAsString();

            StreamSource responseStream = new StreamSource(new StringReader(response));

            JAXBContext jaxbContext = JAXBContext.newInstance(CardDetailsResponse.class);

            javax.xml.bind.Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();

            cardDetailsResponse=(CardDetailsResponse)jaxbUnmarshaller.unmarshal(responseStream);

            System.out.println("object data = "+cardDetailsResponse.getCardListField().getCardDetailsField().getCardNumberField());

        }
        catch (Exception e) {
            e.printStackTrace();
        }
        return cardDetailsResponse;
    }
srihari
  • 397
  • 2
  • 7
  • 18
1

Going by your code, the service you are trying to call is a JSON HTTP service. Consuming such services is what the Spring RestTemplate was designed to simplify.

There is a guide on how to consume REST web services at spring.io.

The code for your call, using RestTemplate is as follows:

RestTemplate restTemplate = new RestTemplate();
CardDetailsResponse cardDetails = restTemplate.getForObject(
        "https://sriharicorp.com/sampleApplications/CoreIssue.aspx?"
        + "serviceName=svc"
        + "&loginStatus=1"
        + "&accountNumber=32146546454", 
    CardDetailsResponse.class);
Steve
  • 9,270
  • 5
  • 47
  • 61