How to return only an AutoComplete instance in java?

76
June 20, 2019, at 1:20 PM

I am coming to an issue where I am getting three objects instead of one object. (See my schema and the output result I am getting below). All I need to do is return a list of the Object Mapper that transforms the collection in an array. In my controller this is the cause "public @ResponseBody List" please help me to have it print like my schema. Thanks!

Controller:

@RestController
        public class AutoCompleteController {
            private AutoCompleteService autoCompleteService;
            private EntityManager em;
            public AutoCompleteController(AutoCompleteService autoCompleteService, EntityManager em){
                this.autoCompleteService = autoCompleteService;
                this.em = em;
            }
            @RequestMapping(value = "jobs", method = RequestMethod.GET)
           public @ResponseBody List<AutoComplete> getSalary(@RequestParam(value = "autocomplete")
                                                                        String autocompleteValue) {
              return autoCompleteService.retrieveSalary(autocompleteValue);
          }
            public void getAllSalaries(HttpServletResponse res) {
                Stream<AutoComplete> stream = autoCompleteService.retrieveAllSalaries();
                DataStreamUtility.streamObjects(stream, res, em);
            }
        }
     @RequestMapping(value = "/jobs")
     public @ResponseBody List < AutoComplete > getSalary(@RequestParam(value = "autocomplete", defaultValue = "1400") String autocompleteValue) {
      return autoCompleteService.retrieveSalary(autocompleteValue);
     }
     public void getAllSalaries(HttpServletResponse res) {
      Stream < AutoComplete > stream = autoCompleteService.retrieveAllSalaries();
      DataStreamUtility.streamObjects(stream, res, em);
     }
    }

Service:

    @Service
public class AutoCompleteService {
    Logger LOGGER = LogManager.getLogger(com.paysr.citypay.autocomplete.AutoCompleteService.class);
    private AutoCompleteRepository autocompleteRepository;
    public AutoCompleteService(AutoCompleteRepository autRepo) {
        this.autocompleteRepository = autRepo;
    }
    public List<AutoComplete> retrieveSalary(String jobClassCd) {

     List<AutoComplete> salaries = autocompleteRepository.findByJobClassCdStartsWith(jobClassCd);
        if (salaries.size() <= 0) {
            throw new AutoCompleteNotFoundException(jobClassCd);
        }
        return salaries;
    }

    public Stream<AutoComplete> retrieveAllSalaries() {
        return autocompleteRepository.streamAll();

    }

} 

Schema (The way I want to return / result):

    {
            "id":               String,  
            "label":            String, ( 
            "pay_grade_description_link":    String,
            "job_type":         String,  
            "mou":              String,
            "mou_description":  String,
            "special_notes":    String,
            "salary_range_min_step_message": String,

            "hourly": [
                String  
            ],
            "biweekly": [
                String  
            ],
            "annual": [
                String  
            ],

        }  

Result I am getting as of now:

     [
     {
            "id": "1400 ",
            "label": "ADMIN ",
            "pay_grade_description_link": null,
            "job_type": null,
            "mou": "00000",
            "mou_description": "NONE",
            "special_notes": " ",
            "salary_range_min_step_message": null,
            "Annual": [
                "  $20,000.00",
                "  $20,000.00",
                null,
                null,
                null,
                null,
                null
            ]
        }]
        },
    {
            "id": "1400 ",
            "label": "ADMIN ",
            "pay_grade_description_link": null,
            "job_type": null,
            "mou": "00000",
            "mou_description": "NONE",
            "special_notes": " ",
            "salary_range_min_step_message": null,
            "Biweekly": [
                "  $1,000.00",
                "  $1,000.00",
                null,
                null,
                null,
                null,
                null
            ]
        },
        {
            "id": "1400 ",
            "label": "ADMIN ",
            "pay_grade_description_link": null,
            "job_type": null,
            "mou": "00000",
            "mou_description": "NONE",
            "special_notes": " ",
            "salary_range_min_step_message": null,
            "Hourly": [
                "  $14.00",
                "  $14.00",
                null,
                null,
                null,
                null,
                null
            ]
        }
    ]  

AutoComplete:

    @Table(name = "xa_s_xPay", schema = "xxx")
@Entity
@Setter
@JsonSerialize(using = AutoCompleteSerializer.class)
public class AutoComplete {

    @Column(name = "JOB_CLASS_CD")
    public String jobClassCd;
    @Column(name = "JOB_CLASS_TITLE")
    public String jobClassTitle;
    @Column(name = "PAY_GRADE")
    public String payGrade;
    @Column(name = "JOB_GROUP")
    public String jobGroup;
    @Column(name = "MOU")
    public String mou;
    @Column(name = "MOU_DESCRIPTION")
    public String mouDescription;

    @Column(name = "MINIMUM_ANNUAL")
    public String minimumAnnual;
    @Column(name = "MAXIMUM_ANNUAL")
    public String maximumAnnual;
    @Column(name = "HIGHEST_COLUMN_HEADING_TO_DISP")
    public String highestColumnHeadingToDisp;
    @Column(name = "LOWEST_COLUMN_HEADING_TO_DISP")
    public String lowestColumnHeadingToDisp;
    @Column(name = "SECTION_TO_DISPLAY")
    public String sectionToDisplay;
    @Id
    @Column(name = "RATE_TYPE")
    public String rateType;
    @Column(name = "STEP_1")
    public String step1;
    @Column(name = "STEP_2")
    public String step2;
    @Column(name = "STEP_3")
    public String step3;
    @Column(name = "STEP_4")
    public String step4;
    @Column(name = "STEP_5")
    public String step5;
    @Column(name = "STEP_6")
    public String step6;
    @Column(name = "STEP_7")
    public String step7;
    @Column(name = "STEP_8")
    public String step8;
    @Column(name = "STEP_9")
    public String step9;
    @Column(name = "STEP_10")
    public String step10;
    @Column(name = "STEP_11")
    public String step11;
    @Column(name = "STEP_12")
    public String step12;
    @Column(name = "STEP_13")
    public String step13;
    @Column(name = "STEP_14")
    public String step14;
    @Column(name = "STEP_15")
    public String step15;
    @Column(name = "STEP_16")
    public String step16;
    @Column(name = "STEP_17")
    public String step17;
    @Column(name = "STEP_18")
    public String step18;
    @Column(name = "STEP_19")
    public String step19;
    @Column(name = "STEP_20")
    public String step20;
    @Column(name = "FLAT_RATE")
    public String flatRate;
    @Column(name = "PER_EVENT_RATE")
    public String perEventRate;
    @Column(name = "SPECIAL_NOTES")
    public String specialNotes;
    @Column(name = "CREATED_BY")
    public String createdBy;
    @Column(name = "CREATION_DATE")
    public String creationDate;
    @Column(name = "LAST_UPDATED_BY")
    public String lastUpdatedBy;
    @Column(name = "LAST_UPDATE_DATE")
    public String lastUpdatedDate;
    @Column(name = "SALARY_RANGE")
    public String salaryRange;
    @Column(name = "MIN_STEP")
    public String minStep;
    @Column(name = "SALARY_RANGE_MIN_STEP_MESSAGE")
    public String salaryRangeMinStepMessage;

}
Answer 1

It looks like your code is behaving the way it is written. If you only want a single Json object in the response vs an array of Json objects you will need to change the definition of the controller method to something like the following.

 @RequestMapping(value = "/jobs")
 public @ResponseBody AutoComplete getSalary(@RequestParam(value = "autocomplete", defaultValue = "1400") String autocompleteValue) {
  // If you on
  return autoCompleteService.retrieveSalary(autocompleteValue);
 }

And then modify your AutoCompleteRepository and AutoCompleteService to only return a single value from retrieveSalary.

The caveat is that if more than one value is found for a given search you will receive an error. You can see more about repository query return types here.

https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#repository-query-return-types

Here are some other ways to limit the results from a JPA query. https://www.baeldung.com/jpa-limit-query-results

READ ALSO
How to implement arraylist into the ListView?

How to implement arraylist into the ListView?

I'm just getting started with JavaFX which i find pretty cool , so i m trying to implement the arraylist of an ToDListItem class into the listView of my FXML file

56
Calling external api from spring boot with multipart/form-data

Calling external api from spring boot with multipart/form-data

I am working in a project where i need to get the data out of image using ocrI am using ocr by third parties where I can upload file and get the data out of ocr

54
Why does OpenCV&#39;s detectMultiScale3 function hang my program?

Why does OpenCV's detectMultiScale3 function hang my program?

I was playing around with OpenCV Java, and I decided to see if I could output the confidence values that OpenCV's CascadeClassifier returned by running the detectMultiScale3 functionMy code is as follows:

44
How to modify text on a TextArea object from outside of it&#39;s class?

How to modify text on a TextArea object from outside of it's class?

I want to be able to modify the text in a TextArea object while the game is runningHowever, I can't get my code to work, and it feels like there is a proper way to do this that I just can't figure out

52