0

I was facing an issue while converting a bean with request scope to JSON using object mapper. It thows an exception like below. Not sure what am missing. I couldn't find any relatable resource on the web. Can someone explain what should be the way to do this.

Thanks in advance.

Line of code :

planInfo -> Bean with request scope

String json = new ObjectMapper().writeValueAsString(planInfo);

Exception :

"timestampSeconds":1630567890,"timestampNanos":683000000,"severity":"ERROR","thread":"qtp133294616-216","logger":"com.signup.api.service.SignupService","message":"exception \ncom.fasterxml.jackson.databind.JsonMappingException: Infinite recursion (StackOverflowError) (through reference chain: jdk.internal.loader.ClassLoaders$PlatformClassLoader[\"unnamedModule\"]-\u003ejava.lang.Module[\"classLoader\"]-....

PlanInfo

import lombok.Data;
import org.springframework.stereotype.Component;
import org.springframework.web.context.annotation.RequestScope;

@Data
@Component
@RequestScope
public class PlanInfo {
  String additionalPlanDetails;
  String additionalServiceCost;
  String listOfAdditionalServiceIds;

  String planMinutes;
  String planPrice;
  String totalamt;

  String setUpFee;
  String selectedPlanId;
  String canadaProvince;

  String vatAmount;
  String vatPercentage;
  String netTotalAmt;

  String selectedIvrPlanId;
  String selectedIvrPlanMinutes;
  String selectedIvrPlanPrice;

  String totalPlanPrice;
  String totalAmtAfterDiscount;
  boolean waiveSetupFee;
  String planCategory;
}
3
  • Don't try to send a request scoped bean, as you will actually be sending the proxy not the underlying object. As this proxy contains references to context etc. you will get into this issue (assuming @RequestScope here). Also if your bean references bean a, which references bean b, which references bean a, you get a recursive error. Commented Sep 2, 2021 at 7:50
  • Please add the full stack trace (which should be visible in the logging, you only included the error message you get in your webbrowser/client. Commented Sep 2, 2021 at 7:54
  • @M.Deinum, yes, it seems bit hard to do things with proxied bean. Also we don't have any other bean referece in this bean(with request scope) so there is no way for recusive DI here. So, we decided to remove the request scope from this bean and added a new helper bean with request scope. so our helper bean is not coupled with the response. Commented Sep 2, 2021 at 8:31

0

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.