javax.ws.rs.ProcessingException, could not find writer for content-type application/json type, in Payara Server 5

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

javax.ws.rs.ProcessingException, could not find writer for content-type application/json type, in Payara Server 5

问题

Here's the translated version of the provided content:

首先,对于我臃肿的源代码和简单的问题表示抱歉。

我遇到了这个错误。

**javax.ws.rs.ProcessingException: RESTEASY003215: 找不到适用于内容类型 application/json 的 writer 类型: com.acme.customers.lib.v1.Customer**

完整的追踪:

--- exec-maven-plugin:1.5.0:exec (default-cli) @ acme-customers-api ---
"main"线程中的异常 javax.ws.rs.ProcessingException: RESTEASY004655: 无法调用请求: javax.ws.rs.ProcessingException: RESTEASY003215: 找不到适用于内容类型 application/json 的 writer 类型: com.acme.customers.lib.v1.Customer
    	at org.jboss.resteasy.client.jaxrs.engines.ManualClosingApacheHttpClient43Engine.invoke(ManualClosingApacheHttpClient43Engine.java:287)
    	at org.jboss.resteasy.client.jaxrs.internal.ClientInvocation.invoke(ClientInvocation.java:488)
    	...

使用 URL 为 **http://localhost:8080/api/v1/customers/createPurchaser** 的 Postman

[![enter image description here][1]][1]

使用 Header 
[![enter image description here][2]][2]

我的 `ConsumingServices` 类是

    import java.util.Date;
    import javax.ws.rs.client.Client;
    import javax.ws.rs.client.ClientBuilder;
    import javax.ws.rs.client.Entity;
    import javax.ws.rs.client.Invocation;
    import javax.ws.rs.client.WebTarget;
    import javax.ws.rs.core.MediaType;
    import javax.ws.rs.core.Response;
    import org.glassfish.jersey.client.ClientConfig;
    import org.glassfish.jersey.client.ClientProperties;
    
    public class ConsumingServices {
    
        public static void main(String[] args) {
            Customer customer = new Customer();
            customer.setFirstName("John");
            customer.setLastName("Mason");
            customer.setEmail("john.mason@mail.com");
            customer.setDateOfBirth(new Date());
            customer.setStatus(CustomerStatus.ACTIVE);
    
            ClientConfig configuration = new ClientConfig();
            configuration.property(ClientProperties.CONNECT_TIMEOUT, 1000);
            configuration.property(ClientProperties.READ_TIMEOUT, 1000);
            Client client = ClientBuilder.newClient(configuration);
    
            WebTarget target = client.target("http://localhost:8080/api/v1/customers");
    
            Invocation.Builder invocationBuilder = target.path("createPurchaser")
                    .request(MediaType.APPLICATION_JSON)
                    .accept(MediaType.APPLICATION_JSON);
    
            Response response = invocationBuilder
                    .header("Authorization", "1234")
                    .post(Entity.entity(customer, MediaType.APPLICATION_JSON));
    
            ...
        }
    }

`pom.xml` 中的依赖项是

    <dependencies>
        <dependency>
            <groupId>com.acme</groupId>
            <artifactId>acme-customers-lib</artifactId>
            <version>${project.version}</version>
        </dependency>
        ...
    </dependencies>

**问题:我应该如何修复这个异常?**

我的类代码...

`CustomerResource` 类

    @Path("/customers")
    @ApplicationScoped
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    public class CustomerResource {
        @POST
        @Path("/createPurchaser")
        public Response createPurchaser(Customer customer) {
            ...
        }
    }

`CustomerService` 接口

    public interface CustomerService {
        Purchaser createPurchaser(Customer customer);
    }

`CustomerServiceImpl` 类中的 `createPurchaser` 方法

    @ApplicationScoped
    public class CustomerServiceImpl implements CustomerService {
        @Override
        public Purchaser createPurchaser(Customer customer) {
            ...
        }
    }

`Customer` 类

    public class Customer extends BaseType implements Serializable {
        ...
    }

`BaseType` 类

    public class BaseType {
        ...
    }

`CustomerStatus` 枚举

    public enum CustomerStatus {
        ACTIVE, INACTIVE
    }

`Purchaser` 类

    public class Purchaser implements Serializable {
        ...
    }

`PurchaserDocument` 类

    public class PurchaserDocument implements Serializable {
        ...
    }

Please note that some content might be omitted due to the translation limitations of this format.

英文:

first Sorry for my bulky source code and simple question.

I get this error.

javax.ws.rs.ProcessingException: RESTEASY003215: could not find writer for content-type application/json type: com.acme.customers.lib.v1.Customer

Complete Trace:

--- exec-maven-plugin:1.5.0:exec (default-cli) @ acme-customers-api ---
Exception in thread &quot;main&quot; javax.ws.rs.ProcessingException: RESTEASY004655: Unable to invoke request: javax.ws.rs.ProcessingException: RESTEASY003215: could not find writer for content-type application/json type: com.acme.customers.lib.v1.Customer
	at org.jboss.resteasy.client.jaxrs.engines.ManualClosingApacheHttpClient43Engine.invoke(ManualClosingApacheHttpClient43Engine.java:287)
	at org.jboss.resteasy.client.jaxrs.internal.ClientInvocation.invoke(ClientInvocation.java:488)
	at org.jboss.resteasy.client.jaxrs.internal.ClientInvocation.invoke(ClientInvocation.java:65)
	at org.jboss.resteasy.client.jaxrs.internal.ClientInvocationBuilder.post(ClientInvocationBuilder.java:226)
	at com.acme.customers.api.rest.v1.test.ConsumingServices.main(ConsumingServices.java:67)
Caused by: javax.ws.rs.ProcessingException: RESTEASY003215: could not find writer for content-type application/json type: com.acme.customers.lib.v1.Customer
	at org.jboss.resteasy.core.interception.jaxrs.ClientWriterInterceptorContext.throwWriterNotFoundException(ClientWriterInterceptorContext.java:50)
	at org.jboss.resteasy.core.interception.jaxrs.AbstractWriterInterceptorContext.getWriter(AbstractWriterInterceptorContext.java:302)
	at org.jboss.resteasy.core.interception.jaxrs.AbstractWriterInterceptorContext.syncProceed(AbstractWriterInterceptorContext.java:240)
	at org.jboss.resteasy.core.interception.jaxrs.AbstractWriterInterceptorContext.proceed(AbstractWriterInterceptorContext.java:224)
	at org.jboss.resteasy.client.jaxrs.internal.ClientInvocation.writeRequestBody(ClientInvocation.java:440)
	at org.jboss.resteasy.client.jaxrs.engines.ManualClosingApacheHttpClient43Engine.writeRequestBodyToOutputStream(ManualClosingApacheHttpClient43Engine.java:589)
	at org.jboss.resteasy.client.jaxrs.engines.ManualClosingApacheHttpClient43Engine.buildEntity(ManualClosingApacheHttpClient43Engine.java:548)
	at org.jboss.resteasy.client.jaxrs.engines.ManualClosingApacheHttpClient43Engine.loadHttpMethod(ManualClosingApacheHttpClient43Engine.java:455)
	at org.jboss.resteasy.client.jaxrs.engines.ManualClosingApacheHttpClient43Engine.invoke(ManualClosingApacheHttpClient43Engine.java:265)
	... 4 more
Command execution failed.
org.apache.commons.exec.ExecuteException: Process exited with an error: 1 (Exit value: 1)
    at org.apache.commons.exec.DefaultExecutor.executeInternal (DefaultExecutor.java:404)
    at org.apache.commons.exec.DefaultExecutor.execute (DefaultExecutor.java:166)
    at org.codehaus.mojo.exec.ExecMojo.executeCommandLine (ExecMojo.java:764)
    at org.codehaus.mojo.exec.ExecMojo.executeCommandLine (ExecMojo.java:711)
    at org.codehaus.mojo.exec.ExecMojo.execute (ExecMojo.java:289)
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo (DefaultBuildPluginManager.java:137)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:210)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:156)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:148)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:117)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:81)
    at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build (SingleThreadedBuilder.java:56)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute (LifecycleStarter.java:128)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:305)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:192)
    at org.apache.maven.DefaultMaven.execute (DefaultMaven.java:105)
    at org.apache.maven.cli.MavenCli.execute (MavenCli.java:957)
    at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:289)
    at org.apache.maven.cli.MavenCli.main (MavenCli.java:193)
    at sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke (Method.java:498)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced (Launcher.java:282)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch (Launcher.java:225)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode (Launcher.java:406)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main (Launcher.java:347)
------------------------------------------------------------------------
BUILD FAILURE
------------------------------------------------------------------------
Total time:  5.277 s
Finished at: 2020-09-03T10:55:26-05:00
------------------------------------------------------------------------
Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.5.0:exec (default-cli) on project acme-customers-api: Command execution failed.: Process exited with an error: 1 (Exit value: 1) -&gt; [Help 1]

To see the full stack trace of the errors, re-run Maven with the -e switch.
Re-run Maven using the -X switch to enable full debug logging.

For more information about the errors and possible solutions, please read the following articles:
[Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException

Using Postman with URL http://localhost:8080/api/v1/customers/createPurchaser

javax.ws.rs.ProcessingException, could not find writer for content-type application/json type, in Payara Server 5

With the Header
javax.ws.rs.ProcessingException, could not find writer for content-type application/json type, in Payara Server 5

My ConsumingServices class is

import java.util.Date;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.Invocation;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import org.glassfish.jersey.client.ClientConfig;
import org.glassfish.jersey.client.ClientProperties;

public class ConsumingServices {

    public static void main(String[] args) {
        Customer customer = new Customer();
        customer.setFirstName(&quot;John&quot;);
        customer.setLastName(&quot;Mason&quot;);
        customer.setEmail(&quot;john.mason@mail.com&quot;);
        customer.setDateOfBirth(new Date());
        customer.setStatus(CustomerStatus.ACTIVE);

        ClientConfig configuration = new ClientConfig();
        configuration.property(ClientProperties.CONNECT_TIMEOUT, 1000);
        configuration.property(ClientProperties.READ_TIMEOUT, 1000);
        Client client = ClientBuilder.newClient(configuration);

        WebTarget target = client.target(&quot;http://localhost:8080/api/v1/customers&quot;);

        Invocation.Builder invocationBuilder = target.path(&quot;createPurchaser&quot;)
                .request(MediaType.APPLICATION_JSON)
                .accept(MediaType.APPLICATION_JSON);

        Response response = invocationBuilder
                .header(&quot;Authorization&quot;, &quot;1234&quot;)
                .post(Entity.entity(customer, MediaType.APPLICATION_JSON));

        if (response.getStatus() == Response.Status.OK.getStatusCode()) {
            Purchaser purchaser = response.readEntity(Purchaser.class);
            System.out.println(&quot;purchaser:&quot;.concat(purchaser.toString()));
        } else {
            if (MediaType.TEXT_PLAIN_TYPE.equals(response.getMediaType())) {
                String message = response.readEntity(String.class);
                System.out.println(&quot;message:&quot; + message);
            } else if (MediaType.APPLICATION_JSON.equals(response.getMediaType())) {
                ApiError apiError = response.readEntity(ApiError.class);
                System.out.println(&quot;apiError:&quot;.concat(apiError.toString()));
            } else {
                System.out.println(&quot;response.getMediaType():&quot; + response.getMediaType());
                String content = response.readEntity(String.class);
                System.out.println(&quot;message:&quot; + content);
            }
        }
    }
}

The Line with Exception is .post(Entity.entity(customer, MediaType.APPLICATION_JSON));

My Dependencies in the pom.xml are

&lt;dependencies&gt;
    &lt;dependency&gt;
        &lt;groupId&gt;com.acme&lt;/groupId&gt;
        &lt;artifactId&gt;acme-customers-lib&lt;/artifactId&gt;
        &lt;version&gt;${project.version}&lt;/version&gt;
    &lt;/dependency&gt;

    &lt;dependency&gt;
        &lt;groupId&gt;javax&lt;/groupId&gt;
        &lt;artifactId&gt;javaee-api&lt;/artifactId&gt;
    &lt;/dependency&gt;
    
    &lt;dependency&gt;
        &lt;groupId&gt;fish.payara.extras&lt;/groupId&gt;
        &lt;artifactId&gt;payara-embedded-web&lt;/artifactId&gt;
    &lt;/dependency&gt;
    
    &lt;dependency&gt;
        &lt;groupId&gt;com.fasterxml.jackson.jaxrs&lt;/groupId&gt;
        &lt;artifactId&gt;jackson-jaxrs-json-provider&lt;/artifactId&gt;
    &lt;/dependency&gt;
    
    &lt;dependency&gt;
        &lt;groupId&gt;com.h2database&lt;/groupId&gt;
        &lt;artifactId&gt;h2&lt;/artifactId&gt;
    &lt;/dependency&gt;

    &lt;!-- https://mvnrepository.com/artifact/javax/javaee-web-api --&gt;
    &lt;dependency&gt;
        &lt;groupId&gt;javax&lt;/groupId&gt;
        &lt;artifactId&gt;javaee-web-api&lt;/artifactId&gt;
        &lt;version&gt;8.0.1&lt;/version&gt;
        &lt;scope&gt;provided&lt;/scope&gt;
    &lt;/dependency&gt;
    
    &lt;dependency&gt;
        &lt;groupId&gt;javax.ws.rs&lt;/groupId&gt;
        &lt;artifactId&gt;javax.ws.rs-api&lt;/artifactId&gt;
    &lt;/dependency&gt;
    
    &lt;!-- https://mvnrepository.com/artifact/org.glassfish.jersey.core/jersey-client --&gt;
    &lt;dependency&gt;
        &lt;groupId&gt;org.glassfish.jersey.core&lt;/groupId&gt;
        &lt;artifactId&gt;jersey-client&lt;/artifactId&gt;
        &lt;version&gt;2.31&lt;/version&gt;
    &lt;/dependency&gt;
    
    &lt;!-- https://mvnrepository.com/artifact/org.jboss.resteasy/jaxrs-api --&gt;
    &lt;dependency&gt;
        &lt;groupId&gt;org.jboss.resteasy&lt;/groupId&gt;
        &lt;artifactId&gt;jaxrs-api&lt;/artifactId&gt;
        &lt;version&gt;3.0.12.Final&lt;/version&gt;
    &lt;/dependency&gt;
    
    &lt;!-- https://mvnrepository.com/artifact/org.jboss.resteasy/resteasy-client --&gt;
    &lt;dependency&gt;
        &lt;groupId&gt;org.jboss.resteasy&lt;/groupId&gt;
        &lt;artifactId&gt;resteasy-client&lt;/artifactId&gt;
        &lt;version&gt;4.5.6.Final&lt;/version&gt;
    &lt;/dependency&gt;
    
    &lt;!-- https://mvnrepository.com/artifact/org.jboss.resteasy/resteasy-jaxrs --&gt;
    &lt;dependency&gt;
        &lt;groupId&gt;org.jboss.resteasy&lt;/groupId&gt;
        &lt;artifactId&gt;resteasy-jaxrs&lt;/artifactId&gt;
        &lt;version&gt;3.13.0.Final&lt;/version&gt;
    &lt;/dependency&gt;
    
    &lt;!-- https://mvnrepository.com/artifact/org.jboss.resteasy/resteasy-jaxb-provider --&gt;
    &lt;dependency&gt;
        &lt;groupId&gt;org.jboss.resteasy&lt;/groupId&gt;
        &lt;artifactId&gt;resteasy-jaxb-provider&lt;/artifactId&gt;
        &lt;version&gt;4.5.6.Final&lt;/version&gt;
    &lt;/dependency&gt;

    &lt;!-- https://mvnrepository.com/artifact/org.jboss.resteasy/resteasy-jackson2-provider --&gt;
    &lt;dependency&gt;
        &lt;groupId&gt;org.jboss.resteasy&lt;/groupId&gt;
        &lt;artifactId&gt;resteasy-jackson2-provider&lt;/artifactId&gt;
        &lt;version&gt;4.5.6.Final&lt;/version&gt;
    &lt;/dependency&gt;
    
    &lt;!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind --&gt;
    &lt;dependency&gt;
        &lt;groupId&gt;com.fasterxml.jackson.core&lt;/groupId&gt;
        &lt;artifactId&gt;jackson-databind&lt;/artifactId&gt;
        &lt;version&gt;2.11.2&lt;/version&gt;
    &lt;/dependency&gt;
    &lt;!-- https://mvnrepository.com/artifact/javax.xml.bind/jaxb-api --&gt;
    &lt;dependency&gt;
        &lt;groupId&gt;javax.xml.bind&lt;/groupId&gt;
        &lt;artifactId&gt;jaxb-api&lt;/artifactId&gt;
        &lt;version&gt;2.3.1&lt;/version&gt;
    &lt;/dependency&gt;

&lt;/dependencies&gt;

QUESTION: How could I fix this exception?

The code of my classes....

The CustomerResource class

@Path(&quot;/customers&quot;)
@ApplicationScoped
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public class CustomerResource {
    @POST
    @Path(&quot;/createPurchaser&quot;)
    public Response createPurchaser(Customer customer) {
        return Response
            .status(Response.Status.OK)
            .entity(customerService.createPurchaser(customer))
            .type(MediaType.APPLICATION_JSON)
            .build();
    }
}

The CustomerService interface

public interface CustomerService {

    Purchaser createPurchaser(Customer customer);
}

The createPurchaser method of CustomerServiceImpl class

@ApplicationScoped
public class CustomerServiceImpl implements CustomerService {

    @Override
    public Purchaser createPurchaser(Customer customer) {

        if (customer == null) {
            throw new EmptyPayloadException(Customer.class.getSimpleName());
        }
        
        Purchaser client = new Purchaser();
        
        client.setFirstName(customer.getFirstName());
        client.setLastName(customer.getLastName());
        client.setAffiliationDate(new Date());
        client.setCustomerStatus(CustomerStatus.ACTIVE);
        client.setPurchaserDocument(new PurchaserDocument());
        client.getPurchaserDocument().setDateOfBirth(customer.getDateOfBirth());
        client.getPurchaserDocument().setNumberDocument(new Random().nextLong());
        client.getPurchaserDocument().setTypeDocument(&quot;CC&quot;);
        return client;
    }
}

The Customer class

import java.io.Serializable;
import java.util.Date;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class Customer extends BaseType implements Serializable {

    private String firstName;
    private String lastName;
    private CustomerStatus status;
    private String email;
    private Date dateOfBirth;

    public String getFirstName() {
        return firstName;
    }
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
    public String getLastName() {
        return lastName;
    }
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
    public CustomerStatus getStatus() {
        return status;
    }
    public void setStatus(CustomerStatus status) {
        this.status = status;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    public Date getDateOfBirth() {
        return dateOfBirth;
    }
    public void setDateOfBirth(Date dateOfBirth) {
        this.dateOfBirth = dateOfBirth;
    }
}

Now the BaseType class

import java.util.Date;
public class BaseType {

    private String id;
    private Date createdAt;
    private Date updatedAt;

    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public Date getCreatedAt() {
        return createdAt;
    }
    public void setCreatedAt(Date createdAt) {
        this.createdAt = createdAt;
    }
    public Date getUpdatedAt() {
        return updatedAt;
    }
    public void setUpdatedAt(Date updatedAt) {
        this.updatedAt = updatedAt;
    }
}

the CustomerStatus enum

public enum CustomerStatus {
    ACTIVE, INACTIVE
}

The Purchaser class

import java.io.Serializable;
import java.util.Date;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class Purchaser implements Serializable {

    private String firstName;
    private String lastName;
    private Date affiliationDate;
    private CustomerStatus customerStatus;
    private PurchaserDocument purchaserDocument;

    public String getFirstName() {
        return firstName;
    }
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
    public String getLastName() {
        return lastName;
    }
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
    public Date getAffiliationDate() {
        return affiliationDate;
    }
    public void setAffiliationDate(Date affiliationDate) {
        this.affiliationDate = affiliationDate;
    }
    public CustomerStatus getCustomerStatus() {
        return customerStatus;
    }
    public void setCustomerStatus(CustomerStatus customerStatus) {
        this.customerStatus = customerStatus;
    }
    public PurchaserDocument getPurchaserDocument() {
        return purchaserDocument;
    }
    public void setPurchaserDocument(PurchaserDocument purchaserDocument) {
        this.purchaserDocument = purchaserDocument;
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append(&quot;Purchaser{firstName=&quot;).append(firstName);
        sb.append(&quot;, lastName=&quot;).append(lastName);
        sb.append(&quot;, affiliationDate=&quot;).append(affiliationDate);
        sb.append(&quot;, customerStatus=&quot;).append(customerStatus);
        sb.append(&quot;, purchaserDocument=&quot;).append(purchaserDocument);
        sb.append(&#39;}&#39;);
        return sb.toString();
    }
}

The PurchaserDocument class

import java.io.Serializable;
import java.util.Date;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class PurchaserDocument implements Serializable {

    private String typeDocument;
    private Long numberDocument;
    private Date dateOfBirth;

    public String getTypeDocument() {
        return typeDocument;
    }
    public void setTypeDocument(String typeDocument) {
        this.typeDocument = typeDocument;
    }
    public Long getNumberDocument() {
        return numberDocument;
    }
    public void setNumberDocument(Long numberDocument) {
        this.numberDocument = numberDocument;
    }
    public Date getDateOfBirth() {
        return dateOfBirth;
    }
    public void setDateOfBirth(Date dateOfBirth) {
        this.dateOfBirth = dateOfBirth;
    }

}

答案1

得分: 4

问题出在 jersey-client 依赖上。你需要移除它。原因是:当类路径上存在 Jersey 客户端时,通用的 ClientBuilder 将始终变成 JerseyClientBuilder^client-source。你所使用的 JSON 依赖(会自动注册到 RESTEasy 客户端的 resteasy-jackson2-provider)是为 RESTEasy 设计的。因此,Jersey 客户端无法识别它。你可以手动使用以下代码将提供者注册到客户端:

client.register(JacksonJaxbJsonProvider.class);

但是,如我之前所说,只需移除 jersey-clientClientBuilder 就会变成 ResteasyClientBuilder,它会识别 resteasy-jackson2-provider 并自动注册它。除非你确实打算使用 Jersey 客户端,在这种情况下,你需要手动注册提供者,或者添加 Jersey 的自动注册依赖,即 jersey-media-json-jackson

更新

注意,如果你计划在服务器环境中使用客户端,我刚注意到你正在使用 Payara,它在内部使用了 Jersey。因此,服务器已经配备了所有的 Jersey JAR 包。如果你想使用 Jackson 作为提供者,应添加 jersey-media-json-jackson 并在客户端中注册 JacksonFeature。如果不这样做,它将默认使用 JSONB 作为 JSON 提供者。如果你想使用 RESTEasy,那么可以忽略这些。

英文:

The problem is the jersey-client dependency. You need to remove it. Reason: The generic ClientBuilder is built to always become a JerseyClientBuilder when Jersey client is on the classpath. The JSON dependency you have (that automatically registers with the RESTEast client) resteasy-jackson2-provider is for RESTEasy. So Jersey client doesn't recognize it. You could manually register the provider with the client and it should work.

client.register(JacksonJaxbJsonProvider.class);

But like I said, just remove jersey-client and ClientBuilder will become ResteasyClientBuilder and it will recognize the resteasy-jackson2-provider and automatically register it. Unless your goal is to use the Jersey client, in which case, you need to manually register the provider, or add the Jersey auto-register dependency, which is jersey-media-json-jackson.

Update

Note, if you plan on using the client in the server environment, I just noticed that you are using Payara, which uses Jersey under the hood. So the server is already equipped with all the Jersey jars. If you want to use Jackson as your provider, you should add jersey-media-json-jackson and register the JacksonFeature with the client. If you don't do this, it will default to using JSONB as the JSON provider. If you want to use RESTEasy, then you can forget this.

huangapple
  • 本文由 发表于 2020年9月4日 00:31:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/63727978.html
匿名

发表评论

匿名网友

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

确定