我正在使用Spring Boot创建Web应用程序。该应用程序将从客户端(Angular)获取凭据,然后使用这些凭据通过以下代码进行请求。但是,它不起作用,因为似乎所有与配置相关的工作都在应用程序启动后立即执行。那时,soap服务凭据为null,这就是为什么spring在HttpComponentsMessageSender
类上将凭据设置为null的原因。当用户通过凭据登录时,它不使用这些凭据,而是使用旧的凭据。
我正在使用以下代码拨打肥皂服务电话:
这是我的rest控制器的代码,它调用soap客户端(在我的情况下为CustomerRequestClient
):
public class CustomerRequestController {
private final CustomerRequestClient CRClient;
CustomerRequestController(CustomerRequestClient CRClient) {
this.CRClient = CRClient;
}
@RequestMapping(method = RequestMethod.POST, value="/getAllCustomerRequests")
@ResponseBody
List<Object> registerStudent(@RequestBody Login user) {
return CRClient.getAllCustomerRequests(user.getUserName(), user.getPassword());
}
正在拨打电话的客户
public class CustomerRequestClient extends WebServiceGatewaySupport {
private static final Logger log = LoggerFactory.getLogger(CustomerRequestClient.class);
public List<Object> getAllCustomerRequests(String userName, String password) {
//Instantiate Request object
FindAePReqEByDocument aePReq= createAllRequest();
//Soap Service call
WebServiceTemplate template = getWebServiceTemplate();
JAXBElement<AePReqEListType> response = (JAXBElement<AePReqEListType>) template.
marshalSendAndReceive("http://localhost:8086/fmax/ws", aePReq);
//Get AePReqEListType object from jaxb element
AePReqEListType r = (AePReqEListType)response.getValue();
//Business logic come afterwards,,,,
}
private FindAePReqEByDocument createAllRequest() {
FindAePReqEByDocument aePReq = new FindAePReqEByDocument();
AePReqEType aePReqEType = new AePReqEType();
aePReq.setAePReqE(aePReqEType);
SortObjectListType s = new SortObjectListType();
aePReq.setList(s);
return aePReq;
}
}
配置类如下:
public class CustomerRequestConfig {
@Bean
public Jaxb2Marshaller marshaller() {
Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
marshaller.setContextPath("my.wsdl");
return marshaller;
}
@Bean
public CustomerRequestClient customerRequestClient(Jaxb2Marshaller marshaller) {
CustomerRequestClient client = new CustomerRequestClient();
client.setDefaultUri(URI);
client.setMessageSender(httpComponentsMessageSender(client));
client.setMarshaller(marshaller);
client.setUnmarshaller(marshaller);
return client;
}
@Bean
public HttpComponentsMessageSender httpComponentsMessageSender(CustomerRequestClient client) {
HttpComponentsMessageSender httpComponentsMessageSender = new HttpComponentsMessageSender();
httpComponentsMessageSender.setCredentials(usernamePasswordCredentials(client));
return httpComponentsMessageSender;
}
@Bean
public UsernamePasswordCredentials usernamePasswordCredentials(CustomerRequestClient client) {
System.out.println("Password - " + client.getPassword());
return new UsernamePasswordCredentials(client.getUserName(), client.getPassword());
}
}