search

Home  >  Q&A  >  body text

Restriction Java Spring Boot only accepts unique month-year combinations

<p>How to allow entry of only month-year combinations in Java Spring Boot. I need to ensure that the user (Postman client) can enter a record (randomly generated value) for each month of the current year. For example: month: February -> year: 2020 -> generatedValue = <em>random</em>. When the user enters February 2020 again, it throws an exception. I've tried storing the year and month in separate lists to check if the month of that year already exists in the database, but to no avail. First in the RecordService if I want to see if there is no current year entered by the user. For example, if there is no year 2021, then the "if" should add that year to the list so that I can check the month-year combination. It works, but not what I need because the second if always throws a RuntimeException() unless I enter a year that hasn't been entered yet (e.g. there are 2020, 2021, and 2022, but not 2023), so when the user When adding: </p> <pre class="brush:php;toolbar:false;">{ "month": "March", "year": "2023", "readingTime": "1" }</pre> <p>The code will work because there is no year 2023 in the list, but as soon as they try to enter</p> <pre class="brush:php;toolbar:false;">{ "month": "May", "year": "2023", "readingTime": "1" }</pre> <p>It won't work because the year already exists, even though it should work because there is no combination for May 2023 in the database. I tried using a boolean to check if the combination exists but that doesn't make any sense to me. So please help :) Here is my code.</p> <p>RecordController.java</p> <pre class="brush:php;toolbar:false;">@PostMapping("/{client_id}") public Record add(@PathVariable Long client_id, @RequestBody Record record){ return recordService.add(client_id, record); }</pre> <p>RecordService.java</p> <pre class="brush:php;toolbar:false;">public Record add(Long client_id, Record record){ List<String> monthList = months(); List<Integer> yearList = years(); Record recordSave = new Record(); recordSave.setId(client_id); recordSave.setYear(record.getYear()); recordSave.setMonth(record.getMonth()); recordSave.setReadingTime(record.getReadingTime()); recordSave.setReadingValue(randomGenerate()); //Does not work if (!yearList.contains(recordSave.getYear())){ yearList.add(recordSave.getYear()); } //Does not work if (monthList.contains(recordSave.getMonth()) && yearList.contains(recordSave.getYear())){ throw new RuntimeException(); } else { recordRepository.save(recordSave); } return null; }</pre> <p>记录.java</p> <pre class="brush:php;toolbar:false;">@Data @Entity @Table(name = "record") @JsonIgnoreProperties({"hibernateLazyInitializer","handler","device"}) public class Record implements Serializable { @Id @GeneratedValue(strategy= GenerationType.IDENTITY) private Long id; String month; Integer year; String readingTime; Float readingValue; boolean isPresent; }</pre> <p>RandomGenerateValue 和月年函数</p> <pre class="brush:php;toolbar:false;">public Float randomGenerate(){ int max = 105, min = 14; float random_float = (float)Math.floor(Math.random()*(max-min 1) min); return random_float; } public List<String> months(){ List<String> monthList = new ArrayList<>(); monthList.add("January"); monthList.add("February"); monthList.add("March"); monthList.add("April"); monthList.add("May"); monthList.add("June"); monthList.add("July"); monthList.add("August"); monthList.add("September"); monthList.add("October"); monthList.add("November"); monthList.add("December"); return monthList; } public List<Integer> years(){ List<Integer> yearList = new ArrayList<>(); yearList.add(2020); yearList.add(2021); return yearList; }</pre></p>
P粉676588738P粉676588738481 days ago511

reply all(1)I'll reply

  • P粉285587590

    P粉2855875902023-09-04 00:07:58

    Keep the year and month together as a single value

    Your problem seems to stem from treating the year and month as separate values. Instead, combine them. Think in terms of years and months.

    YearMonth Class

    Java contains a class for this concept. It's appropriately named: YearMonth and is located in the java.time package.

    YearMonth ym = YearMonth.of ( 2023 , 3 ) ;

    Or use the Month enumeration to ensure a valid value.

    YearMonth ym = YearMonth.of ( 2023 , Month.MARCH ) ;

    You can use map. Use NavigableMap to keep keys in sorted order.

    NavigableMap< YearMonth , UUID > map = new TreeMap<>() ;
    map.put( 
        YearMonth.of ( 2023 , Month.MARCH ) , 
        UUID.randomUUID()
    );
    map.put( 
        YearMonth.of ( 2023 , Month.FEBRUARY ) , 
        UUID.randomUUID()
    );

    Test whether a value has been entered.

    boolean alreadyPut2023March = map.containsKey( YearMonth.of ( 2023 , Month.MARCH ) ) ;

    ISO 8601

    For text purposes, use the default ISO 8601 format: YYYY-MM

    java.time classes use ISO 8601 format by default when parsing/generating text.

    String iso8601Output = YearMonth.of ( 2023 , Month.MARCH ).toString() ;

    Assuming that your database does not provide the year and month data type, such a string can be used to store in your database. I don't know of any database that provides this data type. So words are enough.

    Please note that when sorting alphabetically, the values ​​will also be sorted chronologically.

    Your JSON will look like this:

    {
        "year-month": "2023-03",
        "readingTime": "1"
    }
    

    reply
    0
  • Cancelreply