我試圖來配置這樣的Web服務客戶端:彈簧自動裝配Autowired在Web服務測試時不工作
@EnableSwagger
@Configuration
@EnableAutoConfiguration
@ComponentScan
@EnableWebMvc
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
隨着第二類配置爲WS:
@Configuration
@ComponentScan(basePackages = {"org.myco.myproj.core.endpoints"})
public class WebServiceConfig {
@Bean
public Jaxb2Marshaller marshaller() throws Exception {
Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
marshaller.setContextPath("org.myco.myproj.core.webservices");
return marshaller;
}
@Bean
public WebServiceTemplate webServiceTemplate(Jaxb2Marshaller marshaller) {
WebServiceTemplate webServiceTemplate = new WebServiceTemplate();
webServiceTemplate.setMarshaller(marshaller);
webServiceTemplate.setUnmarshaller(marshaller);
webServiceTemplate.setDefaultUri("http://localhost:11000/ws/");
return webServiceTemplate;
}
@Bean
public AccountEndpoint accountEndpoint(Jaxb2Marshaller marshaller, WebServiceTemplate webServiceTemplate) {
AccountEndpoint client = new AccountEndpoint(webServiceTemplate);
client.setDefaultUri("http://localhost:11000/ws");
client.setMarshaller(marshaller);
client.setUnmarshaller(marshaller);
return client;
}
}
我已經產生使用JABX從WSDL域類,並創建了這樣的服務端點:
@Service
public class AccountEndpoint extends WebServiceGatewaySupport {
private static final Logger logger = Logger.getLogger(String.valueOf(AccountEndpoint.class));
private WebServiceTemplate webServiceTemplate;
public AccountEndpoint(WebServiceTemplate webServiceTemplate) {
this.webServiceTemplate = webServiceTemplate;
}
public GetAccountResponse getAccount(long accountAgency, long accountNumber) {
GetAccountRequest request = new GetAccountRequest();
request.setAccountAgency(accountAgency);
request.setAccountNumber(accountNumber);
GetAccountResponse response = (GetAccountResponse)
webServiceTemplate.marshalSendAndReceive(request);
return response;
}
}
我創建了一個SIMPL Ë測試,以檢查此運行,這是在自動裝配Autowired場返回NullPointerException
:
@ContextConfiguration("org.myco.myproj.config.WebServiceConfig")
public class AccountEndpointTest extends TestCase {
@Autowired
private AccountEndpoint accountEndpoint;
public void setUp() throws Exception {
super.setUp();
}
@Test
public void testGetAccount() throws Exception {
GetAccountResponse response = accountEndpoint.getAccount(12, 16);
assertNotNull(response);
}
}
什麼我失蹤?謝謝。
感謝您的輸入,這真的很缺失。但是,我仍然無法運行測試。嘗試自動裝載WebServiceTemplate對象是正確的嗎?在調試模式下,我注意到我的AccountEndpoint對象有兩個對象(一個是我創建的,一個是我從WebServiceGatewaySupport繼承而來的)。無論如何,在兩個配置中,我都會收到一個IllegalArgumentException異常:在org.springframework.ws.client.core.WebServiceTemplate.sendAndReceive方法中'uri'一定不能爲空'異常。 – 2014-09-02 14:44:57
@Pedro對不起,我不知道Spring的WS API。你可能想問一個新的問題。 – 2014-09-02 14:51:14