我使用 JABX 从 WSDL 生成了域类并实现了以下客户端:
@Service
public class AccountEndpoint extends WebServiceGatewaySupport {
private static final Logger logger = Logger.getLogger(String.valueOf(AccountEndpoint.class));
public AccountEndpoint() {
}
public GetAccountResponse getAccount(long accountAgency, long accountNumber) {
GetAccountRequest request = new GetAccountRequest();
request.setAccountAgency(accountAgency);
request.setAccountNumber(accountNumber);
GetAccountResponse response = (GetAccountResponse)
getWebServiceTemplate().marshalSendAndReceive(request);
return response;
}
}
我的配置是这样的:
@Configuration
@ComponentScan(basePackages = {"org.myco.mypro.core"})
public class WebServiceConfig {
@Bean
public Jaxb2Marshaller marshaller() throws Exception {
Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
marshaller.setContextPath("org.myco.mypro.webservices");
return marshaller;
}
@Bean
public AccountEndpoint accountEndpoint(Jaxb2Marshaller marshaller) {
AccountEndpoint client = new AccountEndpoint();
client.setDefaultUri("http://localhost:11000/ws");
client.setMarshaller(marshaller);
client.setUnmarshaller(marshaller);
return client;
}
}
和测试:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = WebServiceConfig.class)
public class AccountEndpointTest extends TestCase {
@Autowired
private AccountEndpoint accountEndpoint;
public void setUp() throws Exception {
super.setUp();
}
@Test
public void testGetAccount() throws Exception {
accountEndpoint.setDefaultUri("http://localhost:11000/ws");
Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
marshaller.setContextPath("org.myco.mypro.webservices");
accountEndpoint.setMarshaller(marshaller);
accountEndpoint.setUnmarshaller(marshaller);
GetAccountResponse response = accountEndpoint.getAccount(12, 16);
assertNotNull(response);
}
}
如果我不设置 URI,我会得到:java.lang.IllegalArgumentException: 'uri' 不能为空
;
如果我不设置编码器,我会得到:IllegalStateException:没有注册编码器。检查WebServiceTemplate的配置。
就像 WebServiceConfig 中的配置不起作用,尽管 Autowiring 的 bean 不为 null。
我真的很感谢任何帮助。谢谢。
最佳答案
您应该从 AccountEndpoint 中取出 @Service 注释。 该bean是在配置类中创建的。
关于java - 带有空 URI 和 null Marshaller 的 Spring Web 服务请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25630395/