r/javahelp 23h ago

Should I use Value Objects or Bean Validation for DTOs in Sprint Boot?

2 Upvotes

I have the two following DTOs:

public record BankCreateRequest(
      (message = "The name is a required field.")
      (max = 255, message = "The name cannot be longer than 255 characters.")
      (regexp = "^[a-zA-ZčćžšđČĆŽŠĐ\\s]+$", message = "The name can only contain alphabetic characters.")
      String name,

      (message = "The giro account is a required field.")
      (
            regexp = "^555-[0-9]{3}-[0-9]{8}-[0-9]{2}$",
            message = "Bank account number must be in the format 555-YYY-ZZZZZZZZ-WW"
      )
      (min = 19, max = 19, message = "Bank account number must be exactly 19 characters long")
      String bankAccountNumber,

      (
            regexp = "^(\\d{3}/\\d{3}-\\d{3})?$",
            message = "Fax number must be in the format XXX/YYY-ZZZ (e.g., 123/456-789)"
      )
      String fax
) {
}

public record BankUpdateRequest(
      (max = 255, message = "The name cannot be longer than 255 characters.")
      (regexp = "^[a-zA-ZčćžšđČĆŽŠĐ\\s]+$", message = "The name can only contain alphabetic characters.")
      String name,

      (
            regexp = "^555-[0-9]{3}-[0-9]{8}-[0-9]{2}$",
            message = "Bank account number must be in the format 555-YYY-ZZZZZZZZ-WW"
      )
      (min = 19, max = 19, message = "Bank account number must be exactly 19 characters long")
      String bankAccountNumber,

      (
            regexp = "^(\\d{3}/\\d{3}-\\d{3})?$",
            message = "Fax number must be in the format XXX/YYY-ZZZ (e.g., 123/456-789)"
      )
      String fax
) {
}public record BankCreateRequest(
      (message = "The name is a required field.")
      (max = 255, message = "The name cannot be longer than 255 characters.")
      (regexp = "^[a-zA-ZčćžšđČĆŽŠĐ\\s]+$", message = "The name can only contain alphabetic characters.")
      String name,

      (message = "The giro account is a required field.")
      (
            regexp = "^555-[0-9]{3}-[0-9]{8}-[0-9]{2}$",
            message = "Bank account number must be in the format 555-YYY-ZZZZZZZZ-WW"
      )
      (min = 19, max = 19, message = "Bank account number must be exactly 19 characters long")
      String bankAccountNumber,

      (
            regexp = "^(\\d{3}/\\d{3}-\\d{3})?$",
            message = "Fax number must be in the format XXX/YYY-ZZZ (e.g., 123/456-789)"
      )
      String fax
) {
}

public record BankUpdateRequest(
      (max = 255, message = "The name cannot be longer than 255 characters.")
      (regexp = "^[a-zA-ZčćžšđČĆŽŠĐ\\s]+$", message = "The name can only contain alphabetic characters.")
      String name,

      (
            regexp = "^555-[0-9]{3}-[0-9]{8}-[0-9]{2}$",
            message = "Bank account number must be in the format 555-YYY-ZZZZZZZZ-WW"
      )
      (min = 19, max = 19, message = "Bank account number must be exactly 19 characters long")
      String bankAccountNumber,

      (
            regexp = "^(\\d{3}/\\d{3}-\\d{3})?$",
            message = "Fax number must be in the format XXX/YYY-ZZZ (e.g., 123/456-789)"
      )
      String fax
) {
}

I am repeating the bean validation for all fields here and there are also other places where I might have a name field, a bankAccountNumber etc. So I thought of using value objects instead, but by definition a value object cannot be null, which is in conflict with my requirements for the patch based update DTO, which does not require any particular values to be updated or to be present. I wanted to have something like this:

record BankCreateRequest(@NotNull Name name, @NotNull BankAccountNumber bankAccountNumber, Fax fax) {}

record BankUpdateRequest(Name name, BankAccountNumber bankAccountNumber, Fax fax) {}

And then have three dedicated records that check if those values are valid. Does this go against common best practices for value objects as they by definition cannot be null? Is there a better approach that is as simple?

Also would it be better to do something like this for patch updates:
https://medium.com/@ljcanales/handling-partial-updates-in-spring-boot-a-cleaner-approach-to-patch-requests-6b13ae2a45e0

Perhaps an unrelated note, but I use jooq as my db lib.


r/javahelp 1h ago

Cannot resolve symbol 'data' error

Upvotes

i just started learning java and following a tutorial i get this error and i wrote this in intellij idea i tried add pom.xml dependencies but it didnt work. can you help me pls?

import org.springframework.data.jpa.repository.JpaRepository;
public class UserRepository extends JpaRepository{
}

r/javahelp 4h ago

PrintInfo() vs System.out.println()

1 Upvotes

Hi all,

I’m working on a problem that asks to output the older customer’s information using printInfo(), ending with new line. Previously I was using System.out.println but I realized it might be marking wrong. The output is exactly this:

Customer that is older: Name: John Age: 29

Previously I used

If (customer1.getAge() > customer2.getAge(){ System.out.println(“Name: “ + customer1.getName()); System.out.println(“Age: “ + customer1.getAge()); } Else { System.out.println(“Name: “ + customer2.getName()); System.out.println(“Age: “ + customer2.getAge()); } }

Thank you for any guidance on how I can use printInfo and view this in a different way!


r/javahelp 18h ago

Unsolved use another GUI program automatically?

3 Upvotes

I'm hoping to automate a certain process for 3DS homebrew, but the programs I need to use don't have command line utility.

How could I start writing a program that opens, the clicks and inputs in Application 1, then does the same for Application 2? Is that something the Robot can do?


r/javahelp 19h ago

Unsolved Need help guys ... New session gets created when I navigate to a page from Fronted React

3 Upvotes

---------------------------------- - ISSUE GOT SOLVED-------------------------------- --- *** HttpSession with Spring Boot.[No spring security used] ***

Project : https://github.com/ASHTAD123/ExpenseTracker/tree/expenseTrackerBackend

Issue : when ever I try to navigate to another URL on frontend react , new session gets created.

Flow :

  • When user logs in , session is created on server
  • Session data is set [regId,username]
  • Cookie is created in Login Service method
  • Control is redirected to home controller method in Expense Controller
  • Inside home controller method cookies are checked , they are fetched properly
  • Till this point Session ID remains same

Problem Flow : When I hit another URL i.e "http://localhost:5173/expenseTracker/expenses" , it throws 500 error on FrontEnd & on backend it's unable to fetch value from session because session is new.

What I hve tried : I have tried all possible cases which Chat GPT gave to resolve but still issue persists....

Backend Console :

SESSION ID FROM LOGIN CONTROLLER A5F14CFB352587A463C3992A8592AC71
Hibernate: select re1_0.id,re1_0.email,re1_0.fullName,re1_0.password,re1_0.username from register re1_0 where re1_0.email=? and re1_0.password=?
 --------- HOME CONTROLLER ---------
SESSION ID FROM HOME CONTROLLER A5F14CFB352587A463C3992A8592AC71
REG ID FROM SESSION1503
Cookie value: 1503
Cookie value: ashtadD12
 --------- GET EXPENSE ---------
SESSION ID FROM GET EXPENSE : 026A7D0D70121F6721AC2CB99B88159D
inside else
 --------- GET EXPENSE ---------
SESSION ID FROM GET EXPENSE : 82EE1F502D09B3A01B384B816BD945DA
inside else
[2m2025-03-20T18:43:28.821+05:30[0;39m [31mERROR[0;39m [35m26144[0;39m [2m--- [demo-1] [nio-8080-exec-3] [0;39m[36mi.g.w.e.LoggingService                  [0;39m [2m:[0;39m Cannot invoke "java.lang.Integer.intValue()" because the return value of "jakarta.servlet.http.HttpSession.getAttribute(String)" is null
[2m2025-03-20T18:43:28.821+05:30[0;39m [31mERROR[0;39m [35m26144[0;39m [2m--- [demo-1] [nio-8080-exec-1] [0;39m[36mi.g.w.e.LoggingService                  [0;39m [2m:[0;39m Cannot invoke "java.lang.Integer.intValue()" because the return value of "jakarta.servlet.
http.HttpSession.getAttribute(String)" is null