如何通过在spring应用程序中传递url中的参数来消耗第三方Web服务

时间:2014-12-17 07:34:52

标签: java spring web-services spring-mvc

在我的春季应用程序中,我需要使用以下链接来使用第三方Web服务:How to consume third party WSDL services in Spring MVC。现在我有一个调用Web服务的场景,我不在对象中发送请求。哪个是get方法。所以我必须在URL中添加参数。我怎么能这样做?

这是我的示例网址:

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

5 个答案:

答案 0 :(得分:2)

由于您熟悉Spring,您可以尝试使用Spring类 - RestTemplate

请参阅此处的示例 - http://www.springbyexample.org/examples/contact-rest-services-client.html或此处 - http://www.informit.com/guides/content.aspx?g=java&seqNum=546

答案 1 :(得分:1)

RestTemplate的另一个替代方案是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();
    }

如何将XML解组为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();
  }

}

}

答案 2 :(得分:1)

要使用基于SOAP的Web服务,使用Spring Web Services项目可能会有所帮助。

首先,您需要从服务WSDL生成域对象(类)。

然后,您需要使用两个主要框架类WebServiceGatewaySupportWebServiceTemplate

第一个是您需要扩展以实现自定义服务客户端的抽象类。

第二个类是访问服务数据的便捷模板(它的结构与Spring框架中的其他模板类似;即JdbcTempate)。

查看this教程。

答案 3 :(得分:1)

这是我正在寻找的确切答案

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;
    }

答案 4 :(得分:1)

按照您的代码,您尝试调用的服务是JSON HTTP服务。消费此类服务是Spring RestTemplate旨在简化的过程。

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

使用RestTemplate进行通话的代码如下:

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