java - 带有空 URI 和 null Marshaller 的 Spring Web 服务请求

标签 java spring web-services jdbc

我使用 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/

相关文章:

java - 将 X509 值传递到 LDAP 服务器

javascript - Phonegap、网络服务逻辑?

java - 在什么情况下 JVM 会决定增加堆的大小?

java - 多个实体的通用 JPA 存储库

java - 使用 gson 将对象转换为 json 时无法使 java.lang.reflect.Method 构造函数可访问

java - 尝试将参数传递给 java 应用程序

java - 使用 Spring 可分页的 OpenAPI 生成器

Spring Boot 不读取环境特定的 YAML 文件

java - 使用 jdk 8 的默认 jaxws-ri 实现禁用 SOAP 客户端的 SOAP 验证

web-services - 如何在内网中的两个Web服务之间进行SSO?