Manu
Manu

Reputation: 4137

Spring MVC + Jackson: field not being serialized

I am trying to make a simple round-trip with a REST API that leads to storing an entity into the db and then returns the stored entity. Going down works fine and the entity is stored and correctly returned to the REST Controller. However, when I return it, Jackson seems to serialize it incorrectly, as the "name" attribute is not included.

This is the entity:

@Entity
@Configurable
public class MyEntity extends IdentifiableEntity {

    private String name;

    protected MyEntity() {
    };

    public MyEntity(String name) {
        this.name = name;
    }
}

and the extended entity:

@Configurable
@Inheritance(strategy = InheritanceType.JOINED)
@Entity
public abstract class IdentifiableEntity {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id")
    private Long id;

    @Version
    @Column(name = "version")
    private Integer version = 1;

    public String toString() {
        return ReflectionToStringBuilder.toString(this,
                ToStringStyle.SHORT_PREFIX_STYLE);
    }

    public Long getId() {
        return this.id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Integer getVersion() {
        return this.version;
    }

    public void setVersion(Integer version) {
        this.version = version;
    }
}

The REST controller is:

@RestController
@RequestMapping("/service")
public class Service {

    @RequestMapping(value = "/public/{name}", method = RequestMethod.GET)
    public MyEntity storeEntityPublic(@PathVariable String name) {
        System.out.println("Hello " + name
                + ", I am saving on the db. (PUBLIC)");
        MyEntity saved = controller.saveEntity(name);

        return saved;
    }
}

Then my business logic:

@Service
public class LogicController {
    @Autowired
    private MyEntityRepository myEntityRepository;

    public MyEntity saveEntity(String name) {
        MyEntity cg = new MyEntity(name);

        return myEntityRepository.save(cg);
    }
}

I am using Spring repositories:

@Repository
public interface MyEntityRepository extends JpaSpecificationExecutor<MyEntity>,
        JpaRepository<MyEntity, Long> {

}

The returned JSON is:

{"id":12,"version":1}

Where is my "name" attribute? Is is set in the variable being returned by the REST controller.

Upvotes: 2

Views: 1989

Answers (3)

a better oliver
a better oliver

Reputation: 26828

In response to your "I don't want to have my Entity "dirty"" comment: Jackson allows the use of so-called Mixins. They allow you to define annotations for your class outside the class itself. In your case it could look like this:

public abstract class MyEntityMixin {
  @JsonProperty
  private String name;
}

Upvotes: 0

Lev Kuznetsov
Lev Kuznetsov

Reputation: 3728

You may keep it as a field and annotate the field with @JsonProperty if you like.

Upvotes: 0

Manu
Manu

Reputation: 4137

I found the trick: MyEntity needs to have a public get for the property that has to be shown. A good reason to use a DTO pattern.

Upvotes: 6

Related Questions