2

I have an entity which looks like this (I'll omit getters, setters and constuctors for the sake of brevity):

@Entity
public class System {
    @NotNull
    @Column(name = "SYSTEMID", nullable = false)
    @Embedded
    private SystemId systemId;
}

with SystemId looking like this:

@Embeddable
@JsonSerialize(using = SystemIdSerializer.class)
@JsonDeserialize(using = SystemIdDeserializer.class)
public class SystemId {
    @Column(name = "SYSTEMID", nullable = false)
    private String value;
}

My custom serializer does nothing but write out value, which works for 'normal' JSON, just not the HAL representation which looks like this:

{
    "systemId": {
        "content": "1"
    }
}

I would like to have it like this

{
    "systemId": "1"
}

as well. Is there any way to do it? I've spent some time googling but wasn't successful. Thanks.

1 Answer 1

3

Perhaps the projection can help you...

@Projection(name="withSystemId", types = System.class)
public interface WithSystemId {

    @JsonProperty("systemId")
    @Value("#{target.systemId.value}")
    String getSysId();

    // Other getters of System properties...
}

Then try GET your System:

http://localhost:8080/api/systems?projection=withSystemId

UPDATED

Concerning the custom serializer - just add @JsonUnwrapped annotation to systemId:

@Entity
public class System {

    @Embedded
    @JsonUnwrapped
    private SystemId systemId;
} 

Then you must get what you expect

{
    "systemId": "1"
} 

And I assume that your serializer looks like this:

public class SystemIdSerializer extends StdSerializer<SystemId> {

    public SystemIdSerializer() {
        this(null);
    }

    protected SystemIdSerializer(Class<SystemId> t) {
        super(t);
    }

    @Override
    public void serialize(SystemId id, JsonGenerator gen, SerializerProvider provider) throws IOException {
        gen.writeStartObject();
        gen.writeNumberField("systemId", id.value());
        gen.writeEndObject();
    }
}

Working example is here. See Member and nested MemberSkill classes. Then try to launch the app and GET http://localhost:8080/api/members

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks, I'll try that, but shouldn't it be possible without a projection? When I post content with the custom serializer, Spring is able to deserialize it. I just don't understand why it isn't using the custom serializer.
@N4zroth Maybe it's necessary to register custom serializer explicitly.. I've updated my answer.
@N4zroth no need to register custom serializer, just use @JsonUnwrapped.. I've updated my answer
Awesome, thanks, @JsonUnwrapped is doing exactly what I want. Thanks for the help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.