如何在payara 5上替换jackson for moxy

我读了很多关于如何在payara 5上替换jackson for moxy但是从来没有找到一个好的解决方案,所以我创建了一个小项目并希望有人可以帮助我.

的pom.xml

<dependencies>
    <dependency>
        <groupId>javax</groupId>
        <artifactId>javaee-web-api</artifactId>
        <version>${version.javaee}</version>
        <scope>provided</scope>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.glassfish.jersey.media/jersey-media-json-jackson -->
    <dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-json-jackson</artifactId>
        <version>2.27</version>
    </dependency>

    <dependency>
        <groupId>org.eclipse.microprofile</groupId>
        <artifactId>microprofile</artifactId>
        <type>pom</type>
        <version>1.4</version>
    </dependency>
<dependencies>

App.java

import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;

import org.glassfish.jersey.jackson.JacksonFeature;

@ApplicationPath("/api")
public class App extends Application {

@Override
public Set<Class<?>> getClasses() {
    Set<Class<?>> resources = new java.util.HashSet<>();
    resources.add(JacksonFeature.class);

    resources.add(SimpleService.class);
    return resources;
}

}

SimpleService.java

@Path("sample")
public class SimpleService {

@Path("greet2")
@GET
@Produces(MediaType.APPLICATION_JSON)
public PojoEntity doGreet2() {
    PojoEntity pojoEntity = new PojoEntity();
    pojoEntity.setTeste1("TesteValue1");
    pojoEntity.setTeste2("TesteValue2");
    return pojoEntity;
}
}

PojoEntity.java

public class PojoEntity {


private String teste1;

@JsonProperty("differentName")
private String teste2;

//getters and setters
}

将此微应用程序部署到payara 5并请求端点http://localhost:8080/micro-sample/api/sample/greet2后,结果是(如预期的那样):

{"teste1":"TesteValue1","differentName":"TesteValue2"}

Payara使用的是Jackson而不是moxy. :)太棒了!

==============================================

我的问题是当我使用microprofile到达我自己的端点时:

SimpleServiceMicroprofileApi.java

import javax.enterprise.context.Dependent;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import org.eclipse.microprofile.rest.client.inject.RegisterRestClient;

@Dependent
@RegisterRestClient
@Produces(MediaType.APPLICATION_JSON)
public interface SimpleServiceMicroprofileApi {

    @Path("api/sample/greet2")
    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public PojoEntity recallGreet2();
}

MicroService.java

    package fish.payara.micro.sample;

import java.net.MalformedURLException;
import java.net.URL;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import org.eclipse.microprofile.rest.client.RestClientBuilder;

@Path("micro")
public class MicroService {

    @Path("recallGreet2")
    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public PojoEntity recallGreet2() throws MalformedURLException {
        PojoEntity pojoEntity = new PojoEntity();
        pojoEntity.setTeste1("LOL");
        pojoEntity.setTeste2("LOL2");

        URL apiUrl = new URL("http://localhost:8080/micro-sample");
        SimpleServiceMicroprofileApi playlistSvc = RestClientBuilder.newBuilder().baseUrl(apiUrl)
                .build(SimpleServiceMicroprofileApi.class);

        return playlistSvc.recallGreet2();
    }
}

并在getClasses方法的App.java上添加以下行:

resources.add(MicroService.class);

通过此修改重新部署后,我们可以访问http://localhost:8080/micro-sample/api/micro/recallGreet2,结果是:

{"teste1":"LOL","differentName":null}

显然,microprofile一直使用moxy并忽略PojoEntity属性“differentName”.

在这个例子中,有人知道如何完全取代moxy for jackson吗?

该项目于here开始提供,可以测试这种情况. 🙂

Payara版本:5.183

提前致谢.

最佳答案 您只需要在SimpleServiceMicroprofileApi上注册JacksonFeature:

@RegisterProvider(JacksonFeature.class)

这应该使它工作;)

点赞