2

All my entities inherit from a class named EntidadeBase:

@MappedSuperclass
public abstract class EntidadeBase implements Serializable {

private static final long serialVersionUID = -3912856473903279467L;

@Id
@QueryParam("id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
@PodamStrategyValue(value = NullStrategy.class)
private Long id;

@Column(name = "cadastro_data", nullable = false)
@PodamStrategyValue(value = PastDateStrategy.class)
private LocalDate dataCadastro;

@Column(name = "modificado_data", nullable = false)
@PodamStrategyValue(value = PastDateStrategy.class)
private LocalDate dataModificacao;

@QueryParam("modificado")
@Column(nullable = false)
@PodamBooleanValue(boolValue = false)
private Boolean modificado;

@QueryParam("ativo")
@Column(nullable = false)
@PodamBooleanValue(boolValue = true)
private Boolean ativo;
}

Its a JAX-RS/Jersey Webservice deployed on tomcat 8 that actually use default Jersey impl for POJO binding: MOXy.

My problem is that, for example, when I send a PUT request with a JSON entity inside it, the EntidadeBase fields are not parsed to my EndPoint object

Heres an example:

@PUT
@Override
@Transactional(qualifier = ForTransaction.class)
public Response atualizar(@NotNull Abrangencia entidade) {
    return super.atualizar(entidade);
}

How can I manage to make MOXy parse JSON values to the fields of the inherited superclasses?

Marcos J.C Kichel
  • 6,887
  • 8
  • 38
  • 78

2 Answers2

0

First of all, here it's stated, that you should add dependency, to enable MOXy:

<dependency>
    <groupId>org.glassfish.jersey.media</groupId>
    <artifactId>jersey-media-moxy</artifactId>
    <version>2.16</version>
</dependency>

Then, here you could see example of using MOXy with Jersey. Beans, that you need to parse, should have root annotation @XmlRootElement (even for JSON :)). Also, serializable members of class should have getters and setters defined in the class.

mkrakhin
  • 3,386
  • 1
  • 21
  • 34
  • I had already added the dependency, I them as Xml root elements but that didnt solved anything :( – Marcos J.C Kichel Mar 06 '15 at 12:49
  • @MarcosJ.CKichel Did you add getters and setters? – mkrakhin Mar 06 '15 at 12:50
  • Yes, I did. I just tested it on another project that use Wildfly impl of JEE 7 features, where JAX-RS is based on RestEasy and Json - Pojo Binding is based on Jackson (not 100% sure), and it worked well with the same entity archetype, so I think the problem is based on how Jersey + MOXy do the parse, maybe some config is required, but I cant find it anywhere. Now I am working on setting Jackson as the Json - Pojo Binder. If this dont work I will try to use RestEasy instead of Jersey.. @mkrakhin – Marcos J.C Kichel Mar 06 '15 at 13:29
  • No, usually MOXy doesn't require any additional configuration, just dependency in pom. What version of Jersey do you use? – mkrakhin Mar 06 '15 at 13:42
  • 2.16, I think its the latest, have you already tested this same scenario? @mkrakhin – Marcos J.C Kichel Mar 06 '15 at 14:16
  • @MarcosJ.CKichel yes, it is the latest. Unfortunately I don't have possibility to do that, because posting from cellphone. Regarding your answer, surely packages param is what really solves the problem. Completely forgot about that, sorry :( You could try MOXy with it, should work. – mkrakhin Mar 06 '15 at 14:21
  • dont worry about it, and ty for yor help. I dont think declaring a package param would work since, according to jersey docs, MOXy is the default Json - Pojo Binder, but I will try this later just to make sure, Thank you again :) – Marcos J.C Kichel Mar 06 '15 at 14:39
  • @MarcosJ.CKichel MOXy is default, yes. But still it needs your beans to be registered. Check this example for MOXy: https://github.com/jersey/jersey/tree/2.16/examples/json-moxy/src/main/java/org/glassfish/jersey/examples/jsonmoxy Inside App class it invokes packages method on ResourceConfig. – mkrakhin Mar 06 '15 at 15:01
0

After spending some time on trying to fix my entities, I did some tests using another similar archetype that uses Wildfly 8.2 Impl for the Java EE 7 features (RestEasy for JAX-RS with Jackson for Json-Pojo Bind) and the same entity/resource/repository archetype and that worked like a charm. Then I realized that the problem was MOXy, so I configured my project to use Jackson.

I modified my pom.xml replacing this:

<!-- MOXy -->
<dependency>
    <groupId>org.glassfish.jersey.media</groupId>
    <artifactId>jersey-media-moxy</artifactId>
    <version>${moxy.version}</version>
</dependency>

with this:

<!-- Jackson -->
<dependency>
    <groupId>org.glassfish.jersey.media</groupId>
    <artifactId>jersey-media-json-jackson</artifactId>
    <version>${jackson.version}</version>
</dependency>

and also added this init param to my web.xml:

 <servlet>
    <servlet-name>jax-rs</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>

    <init-param>
       <param-name>org.glassfish.jersey.config.property.packages</param-name>
       <param-value>com.fasterxml.jackson.jaxrs</param-value>
    </init-param>

    ...

Now it works (-;

Arjan Tijms
  • 37,782
  • 12
  • 108
  • 140
Marcos J.C Kichel
  • 6,887
  • 8
  • 38
  • 78