Spring XD OAuth接收器单元测试无法为资源';空';

Spring XD OAuth接收器单元测试无法为资源';空';,oauth,spring-integration,spring-xd,Oauth,Spring Integration,Spring Xd,我正在尝试为我为SpringXD创建的接收器编写一个集成测试。这是我的ModuleConfiguration.java @Configuration @EnableIntegration public class ModuleConfiguration { private OAuth2AccessToken token; @Bean public MessageChannel input() { return new DirectChannel();

我正在尝试为我为SpringXD创建的接收器编写一个集成测试。这是我的ModuleConfiguration.java

@Configuration
@EnableIntegration
public class ModuleConfiguration {

    private OAuth2AccessToken token;

    @Bean
    public MessageChannel input() {
        return new DirectChannel();
    }

    @Bean
    protected OAuth2ProtectedResourceDetails resource() {
        BaseOAuth2ProtectedResourceDetails resource = new BaseOAuth2ProtectedResourceDetails();
        resource.setAccessTokenUri("https://xxxxxxxxxxx");
        resource.setClientId("id");
        resource.setClientSecret("secret");
        resource.setGrantType("authorization_code");
        return resource;
    }

    @Bean
    public OAuth2RestTemplate oAuthTemplate() {
        AccessTokenRequest atr = new DefaultAccessTokenRequest();
        OAuth2RestTemplate restTemplate = new OAuth2RestTemplate(resource(), new DefaultOAuth2ClientContext(atr));
        return restTemplate;
    }

    @Bean
    public HeaderMapper<HttpHeaders> headerMapper() {
        HeaderMapper<HttpHeaders> mapper = new DefaultHttpHeaderMapper();
        HttpHeaders headers = new HttpHeaders();
        token = oAuthTemplate().getAccessToken();
        String authHeader = "Bearer " + token.getValue();

        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.set("Authorization", authHeader);
        mapper.toHeaders(headers);

        return mapper;
    }

    @ServiceActivator(inputChannel="inputChannel")
    @Bean
    public MessageHandler httpout() {
        HttpRequestExecutingMessageHandler handler = new HttpRequestExecutingMessageHandler("https://yyyyyyyyy");
        handler.setCharset("UTF-8");
        handler.setHttpMethod(HttpMethod.POST);
        handler.setHeaderMapper(headerMapper());
        return handler;
    }
}
如果我像这样在ModuleConfiguration中使用GrantType的客户端\u凭据

@Bean
    protected OAuth2ProtectedResourceDetails resource() {
        BaseOAuth2ProtectedResourceDetails resource = new ClientCredentialsResourceDetails();
        resource.setAccessTokenUri("https://xxxxxxx");
        resource.setClientId("id");
        resource.setClientSecret("secret");
        resource.setGrantType("client_credentials");
        return resource;
    }
我明白了

当我只有clientId和secret时,正确的授权类型是什么?我的模块配置中是否缺少某些内容

更新 我将
@ServiceActivator
中的inputChannel更改为正确的值,现在我得到的异常与以前的
BaseOAuth2ProtectedResourceDetails
相同。现在使用
ClientCredentialsResourceDetails

Caused by: java.lang.IllegalArgumentException: Map has no value for 'object-type'
    at org.springframework.web.util.UriComponents$MapTemplateVariables.getValue(UriComponents.java:276)
    at org.springframework.web.util.UriComponents.expandUriComponent(UriComponents.java:221)
    at org.springframework.web.util.HierarchicalUriComponents$FullPathComponent.expand(HierarchicalUriComponents.java:650)
    at org.springframework.web.util.HierarchicalUriComponents.expandInternal(HierarchicalUriComponents.java:319)
    at org.springframework.web.util.HierarchicalUriComponents.expandInternal(HierarchicalUriComponents.java:46)
    at org.springframework.web.util.UriComponents.expand(UriComponents.java:152)
    at org.springframework.web.util.UriComponentsBuilder.buildAndExpand(UriComponentsBuilder.java:376)
    at org.springframework.integration.http.outbound.HttpRequestExecutingMessageHandler.handleRequestMessage(HttpRequestExecutingMessageHandler.java:376)
    ... 35 more

首先,尝试将
OAuthRestTemplate
注入
HttpExecutionMessageHandler

不要使用
headerMapper
,即使像豆子一样。在我看来,这正是我们拥有OAuth
RestTemplate
扩展的原因

另一个问题是接收器及其
服务激活器的通道错误。对,那必须是
输入
bean,但使用与
@ServiceActivator
相同的名称。这是您的
调度程序没有订户的根本原因

@ServiceActivator(inputChannel="inputChannel")
@Bean
public MessageHandler httpout() {
    HttpRequestExecutingMessageHandler handler = new HttpRequestExecutingMessageHandler("https://yyyyyyyyy", oAuthTemplate());
    handler.setCharset("UTF-8");
    handler.setHttpMethod(HttpMethod.POST);
    return handler;
}

如果URL中有一些变量,如
https://foo.bar/{foo}/{bar}
,您必须提供
setUriVariableExpressions(映射uriVariableExpressions)
在运行时根据
requestMessage
解决这些问题:

刚刚更新以进行更正,将在到达我的电脑时修复格式设置。从手机上看不容易
HttpRequestExecutingMessageHandler
中似乎没有针对
restemplate
的setter。最近的一个类似于
setRequestFactory(clienthtprequestfactory)
在这种情况下,我需要创建这种类型的对象,也许我可以在其中插入
restemplate
。如果我没有使用处理程序的
HeaderMapper
,如何插入标题
RestTemplate
没有用于标题的setter。感谢您的
@ServiceActivator
修复程序修复了我的另一个异常,更新了问题<代码>公共HttpRequestExecutingMessageHandler(字符串uri,RestTemplate RestTemplate){
Map没有'object type'的值是另一个问题。它闻起来像是一个新的SO问题:-)。谢谢你,在使用了上面的构造函数之后,我现在得到了
BaseOAuth2ProtectedResourceDetails
子类和grant类型的'Map没有'object type'的值。我将对这个新的异常进行一些研究。
Caused by: org.springframework.integration.MessageDispatchingException: Dispatcher has no subscribers
    at org.springframework.integration.dispatcher.UnicastingDispatcher.doDispatch(UnicastingDispatcher.java:107)
    at org.springframework.integration.dispatcher.UnicastingDispatcher.dispatch(UnicastingDispatcher.java:97)
    at org.springframework.integration.channel.AbstractSubscribableChannel.doSend(AbstractSubscribableChannel.java:77)
    ... 29 more
Caused by: java.lang.IllegalArgumentException: Map has no value for 'object-type'
    at org.springframework.web.util.UriComponents$MapTemplateVariables.getValue(UriComponents.java:276)
    at org.springframework.web.util.UriComponents.expandUriComponent(UriComponents.java:221)
    at org.springframework.web.util.HierarchicalUriComponents$FullPathComponent.expand(HierarchicalUriComponents.java:650)
    at org.springframework.web.util.HierarchicalUriComponents.expandInternal(HierarchicalUriComponents.java:319)
    at org.springframework.web.util.HierarchicalUriComponents.expandInternal(HierarchicalUriComponents.java:46)
    at org.springframework.web.util.UriComponents.expand(UriComponents.java:152)
    at org.springframework.web.util.UriComponentsBuilder.buildAndExpand(UriComponentsBuilder.java:376)
    at org.springframework.integration.http.outbound.HttpRequestExecutingMessageHandler.handleRequestMessage(HttpRequestExecutingMessageHandler.java:376)
    ... 35 more
@ServiceActivator(inputChannel="inputChannel")
@Bean
public MessageHandler httpout() {
    HttpRequestExecutingMessageHandler handler = new HttpRequestExecutingMessageHandler("https://yyyyyyyyy", oAuthTemplate());
    handler.setCharset("UTF-8");
    handler.setHttpMethod(HttpMethod.POST);
    return handler;
}