'how to fetch single attribute from redis cache spring boot?

how to fetch a single attribute from Redis cache spring boot?

I want a response like this one


       {

        "answer": "FHFHFHFHFH" 
       }

but getting this one Please do let me know what should I do?


          {

               "questionId":58,
              "answer":"FHFHFHFHFH"
          }

this is the response I am getting from the above API

below this code which I am using

Controller :->>>



          @GetMapping("/getAnswerStudentExam/{questionId}")
          @ResponseBody
         public ResponseEntity getAnswerStudentExam(@PathVariable Long 
          questionId){
            System.out.println("getAnswerStudentExam  " +questionId);
            QuentionCacheEntity item = answerCache.getAnswerStudent(questionId);
         System.out.println("answerCache.getAnswerStudent(questionId); " +questionId);
            return new ResponseEntity(item, HttpStatus.OK);
         }
    

this API call this method



         @Cacheable(value = "ansCache", key = "#questionId")
         public QuentionCacheEntity getAnswerStudent(Long questionId) {

        QuentionCacheEntity answer = null;
            try {

            answer = ansQuestionRepo.getAnswerFromRedis(questionId);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return answer;
    }

    

this method will call this



          public QuentionCacheEntity getAnswerFromRedis(Long questionId){
             return  (QuentionCacheEntity) hashOperations.get(KEY,questionId);
         }


    

Below is model

code-->>>



          import java.io.Serializable;
          import javax.persistence.Id;
          import lombok.AllArgsConstructor;
         import lombok.Data;
       import lombok.NoArgsConstructor;
        @Data
        @NoArgsConstructor
       @AllArgsConstructor
       public class QuentionCacheEntity implements Serializable {
      @Id
        private Long questionId;

        private String answer;

       }

    


Solution 1:[1]

You could add @JsonIgnore to the questionId field in QuentionCacheEntity (should that be QuestionCacheEntity?) so that when Jackson serializes the object in the ReST controller it knows to ignore that field.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 James Fry