Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to replace jackson for moxy on payara 5

I read a lot about how to replace jackson for moxy on payara 5 but never achieve a good solution, so I create a small project and hope that someone can help me.

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
}

After deploy this micro application into payara 5 and request the endpoint http://localhost:8080/micro-sample/api/sample/greet2 the result is (as expected):

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

Payara is using Jackson instead of moxy. :) Nice!!!

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

My problem is when I use microprofile to reach my own endpoint:

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();
    }
}

And add this line on App.java on getClasses method:

resources.add(MicroService.class);

After the redeploy with this modifications we can access http://localhost:8080/micro-sample/api/micro/recallGreet2 and the result is:

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

Apparently microprofile keeps using moxy and ignore PojoEntity property "differentName".

Anyone know a way to completely replace moxy for jackson in this example?

This project is available here to make it possible to test this situation. :)

Payara version: 5.183

Thanks in advance.

like image 858
tiagomistral Avatar asked Oct 28 '22 00:10

tiagomistral


1 Answers

You just need to register JacksonFeature on your SimpleServiceMicroprofileApi:

@RegisterProvider(JacksonFeature.class)

That should make it work;)

like image 125
yougo Avatar answered Nov 15 '22 06:11

yougo