skip to Main Content

I am a little confused as to why my Spring service is not returning any content. I tried mvn clean install and I also see that the response POJO at the return statement line has actual data when I hit the breakpoint in debugger mode/print to console. Does anyone know what could be the cause?

My @RestController

    @Override
    public MyServiceResponse getDesiredData(MyRequest myRequest)
            throws IOException {
        var resp = new MyServiceResponse();
        var req = new ServiceRequest<MyRequest>();

        req.setPayload(myRequest);

        // this update the pass by reference resp object
        myRequestProcessor.process(req, resp);

        return resp; //at this breakpoint I see data in the debugging console
    }

POJO:

@SuperBuilder
@Data
@NoArgsConstructor
@JsonIgnoreProperties(ignoreUnknown = true)
public class MyServiceResponse {
    SessionInfo sessionInfo;
}

@SuperBuilder
@Data
@NoArgsConstructor
@JsonIgnoreProperties(ignoreUnknown = true)
public class SessionInfo {
    Parameters parameters;
}

@SuperBuilder
@Data
@NoArgsConstructor
@JsonIgnoreProperties(ignoreUnknown = true)
public class Parameters {
    @JsonProperty("zip")
    String zip;

    @JsonProperty("city")
    String city;

    @JsonProperty("state")
    String state;

    @JsonProperty("storeNum")
    Integer storeNum;

    @JsonProperty("startHrs")
    String startHrs;

    @JsonProperty("endHrs")
    String endHrs;
}

2

Answers


  1. Chosen as BEST ANSWER

    It turns out that in my specific case there is another class in the service package that implements the following:

    @Pointcut(value = "execution(public * *(..))")
    public void publicMethod() {
    }
    
    /**
     * @see https://docs.spring.io/spring-framework/docs/5.2.9.RELEASE/spring-framework-reference/core.html#aop-pointcuts-combining
     */
    @Pointcut(value = "within(com.samsclub.ccss.api.service.MyServiceControllerImpl)")
    public void forServiceClass() {
    }
    
    @Around("publicMethod() && forServiceClass()")
    public Object processorLogging(ProceedingJoinPoint proceedingJoinPoint) throws Throwable { ... }
    

    Due to this, the processorLogging is called before and after the API method is called in ControllerImpl. Inside this method the logic did not support a new POJO as the response object so I needed to add that support.


  2. @RestController
    @RequestMapping("/API")
    public class MyController {
    
        @Autowired
        private MyService myService;
    
        @PostMapping("/getData")
        public ResponseEntity<MyServiceResponse> getData(@RequestBody MyRequest myRequest) {
            MyServiceResponse response = myService.getDesiredData(myRequest);
    
            if (response == null || response.getSessionInfo() == null) {
                return ResponseEntity.noContent().build(); // Returns 204 No Content if response is null
            }
    
            return ResponseEntity.ok(response); // Returns 200 OK with JSON response
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search