Java 如何在spring应用程序中通过在url中传递参数来使用第三方Web服务
在我的spring应用程序中,我需要使用以下链接使用第三方web服务:。现在我有了一个调用web服务的场景,其中我不在对象中发送请求。这是一个get方法。因此,我必须向URL添加参数。我该怎么做 这是我的示例URL:Java 如何在spring应用程序中通过在url中传递参数来使用第三方Web服务,java,spring,web-services,spring-mvc,Java,Spring,Web Services,Spring Mvc,在我的spring应用程序中,我需要使用以下链接使用第三方web服务:。现在我有了一个调用web服务的场景,其中我不在对象中发送请求。这是一个get方法。因此,我必须向URL添加参数。我该怎么做 这是我的示例URL: https://sriharicorp.com/sampleApplication/CoreIssue.aspx?user=srihari&password=srihari36&Application=appscale4631&serviceName=svc
https://sriharicorp.com/sampleApplication/CoreIssue.aspx?user=srihari&password=srihari36&Application=appscale4631&serviceName=svc&dbbSystemExtLogin=1&accountNumber=125684364836
因为您熟悉Spring,所以可以尝试使用Spring类-
请参见此处的示例-或此处-因为您熟悉Spring,所以可以尝试使用Spring类-
请参见此处示例-或此处-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解组到对象
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();
}
}
}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解组到对象
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();
}
}
}要使用基于SOAP的web服务,使用project可能会有所帮助 首先,您需要从服务WSDL生成域对象(类) 然后需要使用两个主要的框架类和 第一个是需要扩展的抽象类,以实现定制服务客户机 第二个类是访问服务数据的方便模板(它的结构类似于Spring框架中的其他模板;即
JdbcTempate
)
请参阅教程。要使用基于SOAP的web服务,使用project可能会有所帮助 首先,您需要从服务WSDL生成域对象(类) 然后需要使用两个主要的框架类和 第一个是需要扩展的抽象类,以实现定制服务客户机 第二个类是访问服务数据的方便模板(它的结构类似于Spring框架中的其他模板;即
JdbcTempate
)
看看教程。这正是我想要的答案
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;
}
这正是我想要的答案
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;
}
根据您的代码,您尝试调用的服务是JSON HTTP服务。使用这些服务是Spring
RestTemplate
旨在简化的
有
使用restemplate
调用的代码如下:
RestTemplate restTemplate = new RestTemplate();
CardDetailsResponse cardDetails = restTemplate.getForObject(
"https://sriharicorp.com/sampleApplications/CoreIssue.aspx?"
+ "serviceName=svc"
+ "&loginStatus=1"
+ "&accountNumber=32146546454",
CardDetailsResponse.class);
根据您的代码,您尝试调用的服务是JSON HTTP服务。使用这些服务是Spring
RestTemplate
旨在简化的
有
使用restemplate
调用的代码如下:
RestTemplate restTemplate = new RestTemplate();
CardDetailsResponse cardDetails = restTemplate.getForObject(
"https://sriharicorp.com/sampleApplications/CoreIssue.aspx?"
+ "serviceName=svc"
+ "&loginStatus=1"
+ "&accountNumber=32146546454",
CardDetailsResponse.class);
如何将xml响应放入对象中?这是另一个超出第一个问题范围的问题。检查JAXB及其javax.xml.bind.Marshaller和javax.xml.bind.Unmarshaller。关于另一个问题,如果你认为答案是有效的,就加上一个投票和标记作为结束。但我想得到回应。那么只有我可以投票,我认为如果你再创建一张票子,上面有一个问题,那就是如何在Java中将xml打包成一个对象。如何使用Web服务已经有了答案。无论如何,我会更新我的答案。谢谢你。。我得到了响应..如何将xml响应放入对象?这是另一个超出第一个问题范围的问题。检查JAXB及其javax.xml.bind.Marshaller和javax.xml.bind.Unmarshaller。关于另一个问题,如果你认为答案是有效的,就加上一个投票和标记作为结束。但我想得到回应。那么只有我可以投票,我认为如果你再创建一张票子,上面有一个问题,那就是如何在Java中将xml打包成一个对象。如何使用Web服务已经有了答案。无论如何,我会更新我的答案。谢谢你。。我得到了回应。我已经在使用soap服务了。除了rest模板,还有其他方法吗?rest模板不适用于SOAP服务。这是调用HTTP服务的简单方法,比如您正在询问的HTTP服务。我已经在使用soap服务了。除了rest模板,还有其他方法吗?rest模板不适用于SOAP服务。这是调用HTTP服务的简单方法,例如您正在询问的服务。谢谢您的帖子。我已经在使用“WebServiceTemplate”的项目中使用基于SOAP的web服务。不,我必须调用url中包含参数的服务。我怎样才能做到这一点?谢谢你的帖子。我已经在使用“WebServiceTemplate”的项目中使用基于SOAP的web服务。不,我必须调用url中包含参数的服务。我怎样才能做到这一点?