Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9238919
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T07:52:15+00:00 2026-06-18T07:52:15+00:00

The previous design I had a Spring controller built like follows: @RequestMapping(value = "/rest/funf/{datatype}",

  • 0

The previous design

I had a Spring controller built like follows:

@RequestMapping(value = "/rest/funf/{datatype}", method = RequestMethod.GET)
public ModelAndView getFunfData(@PathVariable("datatype") String dataType,
        WebRequest request) throws HttpException {

I also had a HandlerInterceptorAdapter that performed pre and post-processing (modifying the returned model according to a policy). The signature for the post-process handler is the following

@Override
public void postHandle(HttpServletRequest request,
        HttpServletResponse response, Object handler,
        ModelAndView modelAndView) throws Exception {

It actually worked well. The REST API returned a not-well-structured JSON payload but at least the interceptor (a Policy Enforcement Point) was able to anonymise (strip user personal data) from the model and replaced it.

The point is that with the old design I was able to retrieve response before it was sent to the output, modify and reinject it into the ModelAndView instance. No need to post code for brevity.

The new design

Now I discovered a new design that helps me overcome a flaw in the response structure.

When I had return new ModelAndView(jacksonView_instance, "data", dataToConvertToJson) it was structured like

{
    "data": {
        "myAttr1":"myVal1"
     }
}

which is bad when the receiving program wants to convert the payload to JSON. So I found

@RequestMapping(value = "/rest/funf/{datatype}", method = RequestMethod.GET, produces = { "application/json" })
    public @ResponseBody
    Object[] getFunfData(@PathVariable("datatype") String dataType,
            WebRequest request) throws HttpException {

This simplified code a little and most importantly made the response well structured, like

{
    "myAttr1":"myVal1"
}

That is perfectly OK with REST design contract, but

The problem

Now the HandlerInterceptor won’t be able any more to retrieve the model and thus the object I return from my API (it’s a FUNF-related API that handles personal data, even sensitive, recorded on mobile devices).

So if I need to anonymise records I can’t do it in the interceptor like before

The workaround

I am conscious I can kill the interceptor and perform policy enforcement right in the API, but this is not a great design, even since the PEP that performs authorization in the preHandle and obligation enforcement in the postHandle is currently a Spring object automagically instantiated. Copying and pasting code in the API is a bad idea when dealing with multiple APIs and the possibility in the middle-term to change/extend the PEP implementation.

Anyway, let’s formulate a straight question after having explained the context well

The question is

Given a generic Spring MVC API that returns an object to be handled by a content negotiator

@RequestMapping(value = "/path/{variable}", method = RequestMethod.GET)
public @ResponseBody Object api() {

how can I intercept that returned object in an external class to perform modifications to it? (e.g. replace with a new object instance to be returned to client)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T07:52:16+00:00Added an answer on June 18, 2026 at 7:52 am

    Looks like this is not possible in Spring MVC. I have just seen the code in a debug session.

    When you use ModelAndView, Spring MVC stores the MaV in a variable that lives till postHandle and then is written in response body.

    Instead, when you use @ResponseBody and return value that value is handled directly in the invocation cycle, using a MessageConverter to be written to the response.

    Since the response is a one-way network stream, it’s too late for intercepting it.

    The solution should be based on a different approach than the handlers, like AOP interceptors applied directly to controller methods.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've searched previous SO posts regarding jQuery and responsive design but had no luck
Following on from one of my previous questions to do with method design I
Hi I had an implementation previous versions of iOS for a singleton as follows:
From previous experience I had been under the impression that it's perfectly legal (though
In previous question of mine, someone had meantioned that using Semaphores were expensive in
In my previous question I could add a design time panel to a tab
I had a look at this previous question however this doesn't seem to work
This application was written without any knowledge of design principles (SOLID) by the previous
This is a design question, to whet the guru's appetite. In a previous version,
As per my previous question, here , I've adapted my Data Controller class over

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.