无法在openfire上使用igniterealtime/REST-API-Client创建用户。

huangapple go评论84阅读模式
英文:

Failed to create user at openfire using igniterealtime/REST-API-Client

问题

我正在尝试使用REST API客户端创建用户。我已经按照下面的文档进行了操作:https://github.com/igniterealtime/REST-API-Client

虽然我可以通过 ARC/Postman 创建用户,但当我尝试通过编程方式创建时,我收到以下异常。

以下是我的代码:

AuthenticationToken authenticationToken = new AuthenticationToken(Constants.SHARED_SECRET_KEY);
RestApiClient restApiClient = new RestApiClient(Constants.REST_API_URL, Constants.REST_API_PORT, authenticationToken);

UserDetails userDetails = getUserDetails();
UserEntity userEntity = new UserEntity();
userEntity.setName(userDetails.getName());
userEntity.setEmail(userDetails.getEmail());
userEntity.setPassword(Constants.DEFAULT_PASSWORD);
userEntity.setUsername(userDetails.getName());
restApiClient.createUser(userEntity);

build.gradle:

dependencies {
    implementation fileTree(dir: "libs", include: ["*.jar"])
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'com.google.android.material:material:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'androidx.navigation:navigation-fragment:2.3.0'
    implementation 'androidx.navigation:navigation-ui:2.3.0'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'

    implementation "org.igniterealtime.smack:smack-tcp:4.2.0"
    implementation "org.igniterealtime.smack:smack-experimental:4.2.0"
    implementation ("org.igniterealtime.smack:smack-android:4.2.0") {
        exclude module: 'smack-omemo'
        exclude module: 'smack-omemo-signal'
    }

    implementation ('org.igniterealtime:rest-api-client:1.1.5'){
        exclude module: 'javax.inject'
    }
    implementation 'javax.inject:javax.inject:1'
}

注意:我必须从 org.igniterealtime:rest-api-client:1.1.5 中排除 'javax.inject' 模块,因为它导致了重复的类 javax.inject.Inject,在模块 jetified-javax.inject-1.jar (javax.inject:javax.inject:1) 和 jetified-javax.inject-2.5.0-b05.jar (org.glassfish.hk2.external:javax.inject:2.5.0-b05) 中都有。

异常:
E/SessionManager: 使用 REST 客户端创建用户失败。
org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException: 找不到媒体类型为 application/xml、类型为 org.igniterealtime.restclient.entity.UserEntity 的类 org.igniterealtime.restclient.entity.UserEntity 的 MessageBodyWriter,泛型类型为 class org.igniterealtime.restclient.entity.UserEntity。
...
...
...
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)

英文:

I am trying to create a user using the rest API client. I have followed the below documentation,
https://github.com/igniterealtime/REST-API-Client

Though I am able to create user through ARC/Postman, but when I tried by programatically I am getting below exception.

Below is my code:

AuthenticationToken authenticationToken = new AuthenticationToken(Constants.SHARED_SECRET_KEY);
RestApiClient restApiClient = new RestApiClient(Constants.REST_API_URL, Constants.REST_API_PORT, authenticationToken);

UserDetails userDetails = getUserDetails();
UserEntity userEntity = new UserEntity();
userEntity.setName(userDetails.getName());
userEntity.setEmail(userDetails.getEmail());
userEntity.setPassword(Constants.DEFAULT_PASSWORD);
userEntity.setUsername(userDetails.getName());
restApiClient.createUser(userEntity);  

build.gradle:

dependencies {
implementation fileTree(dir: "libs", include: ["*.jar"])
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'com.google.android.material:material:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'androidx.navigation:navigation-fragment:2.3.0'
implementation 'androidx.navigation:navigation-ui:2.3.0'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'

implementation "org.igniterealtime.smack:smack-tcp:4.2.0"
implementation "org.igniterealtime.smack:smack-experimental:4.2.0"
implementation ("org.igniterealtime.smack:smack-android:4.2.0"){
    exclude module: 'smack-omemo'
    exclude module: 'smack-omemo-signal'
}

implementation ('org.igniterealtime:rest-api-client:1.1.5'){
    exclude module: 'javax.inject'
}
implementation 'javax.inject:javax.inject:1'

}

Note: I have to exclude the 'javax.inject' module from org.igniterealtime:rest-api-client:1.1.5 since it was causing Duplicate class javax.inject.Inject found in modules jetified-javax.inject-1.jar (javax.inject:javax.inject:1) and jetified-javax.inject-2.5.0-b05.jar (org.glassfish.hk2.external:javax.inject:2.5.0-b05)

Exception:
E/SessionManager: Failed to create user with rest client.
org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException: MessageBodyWriter not found for media type=application/xml, type=class org.igniterealtime.restclient.entity.UserEntity, genericType=class org.igniterealtime.restclient.entity.UserEntity.
at org.glassfish.jersey.message.internal.WriterInterceptorExecutor$TerminalWriterInterceptor.aroundWriteTo(WriterInterceptorExecutor.java:247)
at org.glassfish.jersey.message.internal.WriterInterceptorExecutor.proceed(WriterInterceptorExecutor.java:162)
at org.glassfish.jersey.message.internal.MessageBodyFactory.writeTo(MessageBodyFactory.java:1130)
at org.glassfish.jersey.client.ClientRequest.doWriteEntity(ClientRequest.java:517)
at org.glassfish.jersey.client.ClientRequest.writeEntity(ClientRequest.java:499)
at org.glassfish.jersey.client.internal.HttpUrlConnector._apply(HttpUrlConnector.java:393)
at org.glassfish.jersey.client.internal.HttpUrlConnector.apply(HttpUrlConnector.java:285)
at org.glassfish.jersey.client.ClientRuntime.invoke(ClientRuntime.java:252)
at org.glassfish.jersey.client.JerseyInvocation$2.call(JerseyInvocation.java:701)
at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
at org.glassfish.jersey.internal.Errors.process(Errors.java:228)
at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:444)
at org.glassfish.jersey.client.JerseyInvocation.invoke(JerseyInvocation.java:697)
at org.glassfish.jersey.client.JerseyInvocation$Builder.method(JerseyInvocation.java:448)
at org.igniterealtime.restclient.RestClient.call(RestClient.java:153)
at org.igniterealtime.restclient.RestClient.post(RestClient.java:99)
at org.igniterealtime.restclient.RestApiClient.createUser(RestApiClient.java:138)
at com.example.gossip.login.SessionManager.createUser(SessionManager.java:83)
at com.example.gossip.login.LoginActivity$1.onClick(LoginActivity.java:58)
at android.view.View.performClick(View.java:5610)
at android.view.View$PerformClick.run(View.java:22265)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)

答案1

得分: 1

你需要添加 jersey media 依赖。

<dependency>
    <groupId>org.glassfish.jersey.media</groupId>
    <artifactId>jersey-media-jaxb</artifactId>
    <version>2.17</version>
</dependency>
英文:

You need to add the jersey media dependency.

&lt;dependency&gt;
    &lt;groupId&gt;org.glassfish.jersey.media&lt;/groupId&gt;
    &lt;artifactId&gt;jersey-media-jaxb&lt;/artifactId&gt;
    &lt;version&gt;2.17&lt;/version&gt;
&lt;/dependency&gt;

huangapple
  • 本文由 发表于 2020年7月23日 19:52:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/63053621.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定