Skip to content

Fixed issue where JSONView does not work with @RepositoryRestController - #2595

Open
ruthst00 wants to merge 1 commit into
spring-projects:mainfrom
ruthst00:DATAREST-891-ruthes00
Open

Fixed issue where JSONView does not work with @RepositoryRestController#2595
ruthst00 wants to merge 1 commit into
spring-projects:mainfrom
ruthst00:DATAREST-891-ruthes00

Conversation

@ruthst00

@ruthst00 ruthst00 commented Sep 6, 2026

Copy link
Copy Markdown

Fixes #1260

Summary

Issue: spring-data-rest#1161 / DATAREST-891@JsonView annotations on @RepositoryRestController methods were silently ignored, causing all fields to be serialized regardless of the view constraint.

Root Cause: Spring MVC's JsonViewResponseBodyAdvice was not registered in the RepositoryRestHandlerAdapter (the custom RequestMappingHandlerAdapter used by Spring Data REST). Without it, the @JsonView hint set by RequestResponseBodyMethodProcessor was never picked up during response serialization.

Changes Made

1. Core Fix — RepositoryRestMvcConfiguration.java

  • Added import org.springframework.web.servlet.mvc.method.annotation.JsonViewResponseBodyAdvice
  • Registered new JsonViewResponseBodyAdvice() in the ResponseBodyAdvice list of repositoryExporterHandlerAdapter()

2. Test Infrastructure — new files in spring-data-rest-tests-jpa

  • PersonViews.java — Jackson @JsonView marker interfaces (Summary and Full) for Person
  • PersonSummaryController.java — A @RepositoryRestController with a GET /people/summary endpoint annotated with @JsonView(PersonViews.Summary.class), demonstrating the fix
  • Person.java — Added @JsonView(PersonViews.Summary.class) to firstName and lastName fields

3. Test — JpaWebTests.java

  • Registered PersonSummaryController in JpaContextInitializer
  • Added honorsJsonViewOnRepositoryRestControllerMethod() test that verifies firstName/lastName are present and gender/created/siblings/father are absent from the summary view response

The new test (Tests run: 1, Failures: 0, Errors: 0) confirms the fix works correctly.


  • You have read the Spring Data contribution guidelines.
  • You use the code formatters provided here and have them applied to your changes. Don’t submit any formatting related changes.
  • You submit test cases (unit or integration tests) that back your changes.
  • You added yourself as author in the headers of the classes you touched. Amend the date range in the Apache license header if needed. For new types, add the license header (copy from another file and set the current year only).

…not work with @RepositoryRestController.

Signed-off-by: ruthes00 <ruthes00@gmail.com>
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Sep 6, 2026
@ruthst00 ruthst00 changed the title DATAREST-891-ruthes00. made updates to fix issue where JSONView does not work with @RepositoryRestController. Fixed issue where JSONView does not work with @RepositoryRestController Sep 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

status: waiting-for-triage An issue we've not yet triaged

Projects

None yet

Development

Successfully merging this pull request may close these issues.

@JSONView does not work with @RepositoryRestController [DATAREST-891]

2 participants