无法将对象 'org.springframework.cloud.service.BaseServiceInfo@31f7615f' 转换为 class 'com.acme.oauth.SsoServiceInfo'
Cannot cast object 'org.springframework.cloud.service.BaseServiceInfo@31f7615f' to class 'com.acme.oauth.SsoServiceInfo'
我有一个正在尝试绑定到 SSO 服务的 grails 应用程序。当我执行 cf env myapp
时,我可以看到以下内容。
"p-identity": [
{
"credentials": {
"auth_domain": "https://someurl",
"client_id": "968c",
"client_secret": "b44df"
},
"label": "p-identity",
"name": "sso",
"plan": "ssotest",
"provider": null,
"syslog_drain_url": null,
"tags": [],
"volume_mounts": []
}
]
为了与服务绑定,我使用了以下 springcloud 库:
compile (group: 'org.springframework.cloud', name: 'spring-cloud-cloudfoundry-connector', version: '1.2.3.RELEASE' ) {
excludes 'slf4j-api', 'slf4j-log4j12', 'slf4j'
}
compile group: 'org.springframework.cloud', name: 'spring-cloud-spring-service-connector', version: '1.2.3.RELEASE'
然后我创建两个 classes:SsoServiceInfo
和 SsoServiceInfoCreator
SsoServiceInfo.java
package com.acme.cfservice;
import org.springframework.cloud.service.BaseServiceInfo;
public class SsoServiceInfo extends BaseServiceInfo {
public static final String P_SSO_ID = "p-identity";
private String clientId;
private String clientSecret;
private String authDomain;
public SsoServiceInfo(String clientId, String clientSecret, String authDomain) {
super(P_SSO_ID);
this.clientId = clientId;
this.clientSecret = clientSecret;
this.authDomain = authDomain;
}
public String getClientId() {
return clientId;
}
public String getClientSecret() {
return clientSecret;
}
public String getAuthDomain() {
return authDomain;
}
}
SsoServiceInfoCreator.java
package com.acme.cfservice;
import org.springframework.cloud.cloudfoundry.CloudFoundryServiceInfoCreator;
import org.springframework.cloud.cloudfoundry.Tags;
import java.util.Map;
public class SsoServiceInfoCreator extends CloudFoundryServiceInfoCreator<SsoServiceInfo> {
public SsoServiceInfoCreator() {
super(new Tags());
}
@Override
public boolean accept(Map<String, Object> serviceData) {
return serviceData.get("label").equals(SsoServiceInfo.P_SSO_ID);
}
@Override
public SsoServiceInfo createServiceInfo(Map<String, Object> serviceData) {
Map<String, Object> credentials = getCredentials(serviceData);
String clientId = (String) credentials.get("client_id");
String clientSecret = (String) credentials.get("client_secret");
String authDomain = (String) credentials.get("auth_domain");
return new SsoServiceInfo(clientId, clientSecret, authDomain);
}
}
我在 web-app/META-INF/services
中创建了一个名为 org.springframework.cloud.cloudfoundry.CloudFoundryServiceInfoCreator
的文件,并将以下行放入其中
com.acme.cfservice.SsoServiceInfoCreator
问题
但是,当我部署我的应用程序时,我在以下代码行中遇到错误:
def ssoService = (SsoServiceInfo) new CloudFactory().cloud?.getServiceInfo("sso");
我得到的错误是:
OUT org.springframework.beans.factory.BeanCreationException: Error
creating bean with name 'pluginManager' defined in ServletContext
resource [/WEB-INF/applicationContext.xml]: Invocation of init method
failed; nested exception is
org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot
cast object
'org.springframework.cloud.service.BaseServiceInfo@6d2369a8' with
class 'org.springframework.cloud.service.BaseServiceInfo' to class
'com.acme.oauth.SsoServiceInfo'
我应该在这里做什么不同的事情?我的 class SsoServiceInfo
明明扩展了 org.springframework.cloud.service.BaseServiceInfo
,那为什么我得到 CastException
?
您的 SsoServiceInfoCreator.accept
方法与您显示的负载不一致。具体来说,您的有效负载中没有 "label" 。因此 FallbackBaseServiceInfoCreator.java
开始创建 BaseServiceInfo
。您可能希望使用服务负载的一些其他属性来实现 accept
方法。
我有一个正在尝试绑定到 SSO 服务的 grails 应用程序。当我执行 cf env myapp
时,我可以看到以下内容。
"p-identity": [
{
"credentials": {
"auth_domain": "https://someurl",
"client_id": "968c",
"client_secret": "b44df"
},
"label": "p-identity",
"name": "sso",
"plan": "ssotest",
"provider": null,
"syslog_drain_url": null,
"tags": [],
"volume_mounts": []
}
]
为了与服务绑定,我使用了以下 springcloud 库:
compile (group: 'org.springframework.cloud', name: 'spring-cloud-cloudfoundry-connector', version: '1.2.3.RELEASE' ) {
excludes 'slf4j-api', 'slf4j-log4j12', 'slf4j'
}
compile group: 'org.springframework.cloud', name: 'spring-cloud-spring-service-connector', version: '1.2.3.RELEASE'
然后我创建两个 classes:SsoServiceInfo
和 SsoServiceInfoCreator
SsoServiceInfo.java
package com.acme.cfservice;
import org.springframework.cloud.service.BaseServiceInfo;
public class SsoServiceInfo extends BaseServiceInfo {
public static final String P_SSO_ID = "p-identity";
private String clientId;
private String clientSecret;
private String authDomain;
public SsoServiceInfo(String clientId, String clientSecret, String authDomain) {
super(P_SSO_ID);
this.clientId = clientId;
this.clientSecret = clientSecret;
this.authDomain = authDomain;
}
public String getClientId() {
return clientId;
}
public String getClientSecret() {
return clientSecret;
}
public String getAuthDomain() {
return authDomain;
}
}
SsoServiceInfoCreator.java
package com.acme.cfservice;
import org.springframework.cloud.cloudfoundry.CloudFoundryServiceInfoCreator;
import org.springframework.cloud.cloudfoundry.Tags;
import java.util.Map;
public class SsoServiceInfoCreator extends CloudFoundryServiceInfoCreator<SsoServiceInfo> {
public SsoServiceInfoCreator() {
super(new Tags());
}
@Override
public boolean accept(Map<String, Object> serviceData) {
return serviceData.get("label").equals(SsoServiceInfo.P_SSO_ID);
}
@Override
public SsoServiceInfo createServiceInfo(Map<String, Object> serviceData) {
Map<String, Object> credentials = getCredentials(serviceData);
String clientId = (String) credentials.get("client_id");
String clientSecret = (String) credentials.get("client_secret");
String authDomain = (String) credentials.get("auth_domain");
return new SsoServiceInfo(clientId, clientSecret, authDomain);
}
}
我在 web-app/META-INF/services
中创建了一个名为 org.springframework.cloud.cloudfoundry.CloudFoundryServiceInfoCreator
的文件,并将以下行放入其中
com.acme.cfservice.SsoServiceInfoCreator
问题
但是,当我部署我的应用程序时,我在以下代码行中遇到错误:
def ssoService = (SsoServiceInfo) new CloudFactory().cloud?.getServiceInfo("sso");
我得到的错误是:
OUT org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'pluginManager' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Invocation of init method failed; nested exception is org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object 'org.springframework.cloud.service.BaseServiceInfo@6d2369a8' with class 'org.springframework.cloud.service.BaseServiceInfo' to class 'com.acme.oauth.SsoServiceInfo'
我应该在这里做什么不同的事情?我的 class SsoServiceInfo
明明扩展了 org.springframework.cloud.service.BaseServiceInfo
,那为什么我得到 CastException
?
您的 SsoServiceInfoCreator.accept
方法与您显示的负载不一致。具体来说,您的有效负载中没有 "label" 。因此 FallbackBaseServiceInfoCreator.java
开始创建 BaseServiceInfo
。您可能希望使用服务负载的一些其他属性来实现 accept
方法。