Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow @JsonSerialize to be used on @Controller methods [SPR-13369] #17953

Closed
spring-projects-issues opened this issue Aug 19, 2015 · 5 comments
Closed
Labels
status: bulk-closed An outdated, unresolved issue that's closed in bulk as part of a cleaning process

Comments

@spring-projects-issues
Copy link
Collaborator

spring-projects-issues commented Aug 19, 2015

Phil Webb opened SPR-13369 and commented

Similar to the @JsonView support it would be helpful if the @JsonSerialize annotation could be used on a @Controller method.

Possibly raised before in #13935


Issue Links:

@spring-projects-issues
Copy link
Collaborator Author

Sébastien Deleuze commented

That would be indeed useful, but I am not sure Jackson API allows us to specify a JsonSerializer to use just for serializing a specific value.

We currently use writerWithView(Class<?> serializationView) or writer(FilterProvider filterProvider) to get an ObjectWriter instance specific to the value to serialize, but it seems there is no equivalent for JsonSerializer and ObjectWriter itself does not seem to allow to customize that.

That makes me think that even if you can annotate POJOs with @JsonSerialize, that's still something that remains mostly static for Jackson, not dynamic like @JsonView or filters ...

Any thoughts ?

@spring-projects-issues
Copy link
Collaborator Author

Phil Webb commented

Perhaps a custom com.fasterxml.jackson.databind.JsonSerializable could be passed to the ObjectWriter? It could wrap the the response object, create the JsonSerializer instance then from the annotation and delegate the serialize... methods.

@spring-projects-issues
Copy link
Collaborator Author

Dmitry F. commented

it would be best future

@spring-projects-issues
Copy link
Collaborator Author

Jon Edvardsson commented

I am looking for the same feature. Infact, I think it can be done quite easily by just some minor tweaks of the existing code.

Consider that we add support for JsonSerialize.as, then we need to add a field writerFor in MappingJacksonValue. We need to create a new class JsonSerializeResponseBodyAdvice similar to the JsonViewResponseBodyAdvice that copies this info over to MappingJacksonValue.

Then finally update the AbstractJackson2HttpMessageConverter#writeInternal to:

// ... code left out before
        ObjectWriter objectWriter;
        if (filters != null) {
            objectWriter = this.objectMapper.writer(filters);
        }
        else if (writerFor != null) {
            objectWriter = this.objectMapper.writerFor(writerFor);
        }
        else if (javaType != null && javaType.isContainerType()) {
            objectWriter = objectWriter.withType(javaType);
        else {
            
            objectWriter = this.objectMapper.writer();
        }

        if (serializationView != null) {
            objectWriter = objectWriter.withView(serializationView);
        }
        objectWriter.writeValue(generator, value);
//... code left out after

@spring-projects-issues spring-projects-issues added status: waiting-for-triage An issue we've not yet triaged or decided on type: enhancement A general enhancement labels Jan 11, 2019
@spring-projects-issues spring-projects-issues removed the type: enhancement A general enhancement label Jan 11, 2019
@rstoyanchev rstoyanchev added status: bulk-closed An outdated, unresolved issue that's closed in bulk as part of a cleaning process and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Jan 11, 2019
@spring-projects-issues
Copy link
Collaborator Author

Bulk closing outdated, unresolved issues. Please, reopen if still relevant.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: bulk-closed An outdated, unresolved issue that's closed in bulk as part of a cleaning process
Projects
None yet
Development

No branches or pull requests

2 participants