circle
circle
JCHJava Community | Help. Code. Learn.
Created by circle on 11/16/2024 in #java-help
ResponseEntity incorrectly maps values
No description
49 replies
JCHJava Community | Help. Code. Learn.
Created by circle on 11/15/2024 in #java-help
Unable to catch MethodArgumentTypeMismatchException on doFilter()
I have a filter that basically logs incoming and outgoing information, but it seems that using this custom RequestLoggingFilter implements Filter creates a problem, I get MethodArgumentTypeMismatchException when a request's URI is unmapped. Error as follows:
org.springframework.web.method.annotation.MethodArgumentTypeMismatchException: Failed to convert value of type 'java.lang.String' to required type 'java.lang.Long'; For input string: "all"
...
dev.realtards.kuenyawz.filters.RequestLoggingFilter.doFilter(RequestLoggingFilter.java:54) ~[classes/:na]
...
Caused by: java.lang.NumberFormatException: For input string: "all"
org.springframework.web.method.annotation.MethodArgumentTypeMismatchException: Failed to convert value of type 'java.lang.String' to required type 'java.lang.Long'; For input string: "all"
...
dev.realtards.kuenyawz.filters.RequestLoggingFilter.doFilter(RequestLoggingFilter.java:54) ~[classes/:na]
...
Caused by: java.lang.NumberFormatException: For input string: "all"
I believe adding the non-existent URI "all" which I don't even map caused this problem. I find it weird since I could not catch the exception in:
15 replies
JCHJava Community | Help. Code. Learn.
Created by circle on 11/10/2024 in #java-help
Bypass SQLRestriction to achieve SoftDelete with "hard delete"
I have this entity:
@Entity
@SQLRestriction("deleted = false")
@Getter
@Setter
@EqualsAndHashCode(callSuper = true)
@ToString(callSuper = true)
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class Product extends Auditables {

@Id
@SnowFlakeIdValue(name = "product_id")
@Column(name = "product_id", columnDefinition = "BIGINT", updatable = false, nullable = false)
private Long productId;

@Column
private String name;

...

@Column(name = "deleted", nullable = false)
private boolean deleted = false;

@Version
private Long version;

@OneToMany(mappedBy = "product", fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true)
@JsonManagedReference
private Set<Variant> variants = new HashSet<>();

@OneToMany(mappedBy = "product", fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true)
@JsonManagedReference
private Set<ProductImage> images = new HashSet<>();

...
}
@Entity
@SQLRestriction("deleted = false")
@Getter
@Setter
@EqualsAndHashCode(callSuper = true)
@ToString(callSuper = true)
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class Product extends Auditables {

@Id
@SnowFlakeIdValue(name = "product_id")
@Column(name = "product_id", columnDefinition = "BIGINT", updatable = false, nullable = false)
private Long productId;

@Column
private String name;

...

@Column(name = "deleted", nullable = false)
private boolean deleted = false;

@Version
private Long version;

@OneToMany(mappedBy = "product", fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true)
@JsonManagedReference
private Set<Variant> variants = new HashSet<>();

@OneToMany(mappedBy = "product", fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true)
@JsonManagedReference
private Set<ProductImage> images = new HashSet<>();

...
}
30 replies
JCHJava Community | Help. Code. Learn.
Created by circle on 11/9/2024 in #java-help
Populate Spring Data JPA before each @Test
I want to test my Products repository (or other tests that requires database to be populated first). I have tried using ApplicationListener<ApplicationReadyEvent> previously but even though (in the testing environment), the populating methods seem to have been run, the .save methods called by them returns null for each data creation when populating. My goal is to have the database bootstrapped with the data I have parsed from my CSV parser so it can be used as a Test resource. I have tried so many things, but yet none of them automatic populators work, for it to work, I have to manually populate the data explicitly before tests in the test environment. The DataJpaTest:
@Slf4j
@DataJpaTest
public class ProductRepositoryTest {

@Autowired
private ProductRepository productRepository;

@MockBean
private ProductService productService;

@MockBean
private ProductCsvImportServiceImpl productCsvImportService;

@BeforeEach
void setUp() {
productRepository.deleteAll();
productRepository.flush();

CSVParser csvParser = new CSVParser(productService, productCsvImportService);
csvParser.saveProductsFromCsv("seeders/ProductsSeeder.csv");
}

@Test
void testFindAll() {
List<Product> productList = productRepository.findAll();

// Assertions
assertThat(productList.size()).isEqualTo(45);
}
@Slf4j
@DataJpaTest
public class ProductRepositoryTest {

@Autowired
private ProductRepository productRepository;

@MockBean
private ProductService productService;

@MockBean
private ProductCsvImportServiceImpl productCsvImportService;

@BeforeEach
void setUp() {
productRepository.deleteAll();
productRepository.flush();

CSVParser csvParser = new CSVParser(productService, productCsvImportService);
csvParser.saveProductsFromCsv("seeders/ProductsSeeder.csv");
}

@Test
void testFindAll() {
List<Product> productList = productRepository.findAll();

// Assertions
assertThat(productList.size()).isEqualTo(45);
}
45 replies
JCHJava Community | Help. Code. Learn.
Created by circle on 10/27/2024 in #java-help
MapStruct updater not compiling properly
No description
11 replies
JCHJava Community | Help. Code. Learn.
Created by circle on 4/14/2024 in #java-help
Repository bean not found for service implementation
I'm new in spring boot, I came across this problem where I couldn't run the app TLDR of the error:
Description:

Parameter 0 of constructor in dev.vianneynara.portoapi.service.ProjectServiceImpl required a bean of type 'dev.vianneynara.portoapi.repository.ProjectRepository' that could not be found.


Action:

Consider defining a bean of type 'dev.vianneynara.portoapi.repository.ProjectRepository' in your configuration.
Description:

Parameter 0 of constructor in dev.vianneynara.portoapi.service.ProjectServiceImpl required a bean of type 'dev.vianneynara.portoapi.repository.ProjectRepository' that could not be found.


Action:

Consider defining a bean of type 'dev.vianneynara.portoapi.repository.ProjectRepository' in your configuration.
I have tried debugging it, but I couldn't read and solve the problem, as I've reached with copilot to confirm whether or not I did this right, they even said my structure and annotations seem fine.
54 replies
JCHJava Community | Help. Code. Learn.
Created by circle on 1/2/2024 in #java-help
Which methods are being used as an abstract method for functional interface?
No description
6 replies
JCHJava Community | Help. Code. Learn.
Created by circle on 1/2/2024 in #java-help
Make array container classes behave like ArrayList
How to make a class that contains a variable that stores an array able to return the inner element holder like elementData in ArrayList. For example when we want to iterate in enhanced for loop (var e : elements) where elements is an ArrayList , it somehow converts the elements to array to be iterated. I couldn't find documentations online or find discussions about it. Using toArray doesn't seem to work
17 replies
JCHJava Community | Help. Code. Learn.
Created by circle on 5/25/2023 in #java-help
Object sequential searching using compareTo()
I have this simple method to search for an object that uses Comparable interface. But I'm wondering, why does my parameter should use extends instead of implements? Doesn't this break the law where an object can only extend ONE class, but is able to implement multiple interfaces? So why doesn't implements work here?
public static <T extends Comparable<T>> int sequentialSearch(T[] list, T key) {
for (int i = 0; i < list.length; i++) {
if (list[i].compareTo(key) == 0)
return i;
}
return -1;
}
public static <T extends Comparable<T>> int sequentialSearch(T[] list, T key) {
for (int i = 0; i < list.length; i++) {
if (list[i].compareTo(key) == 0)
return i;
}
return -1;
}
27 replies
JCHJava Community | Help. Code. Learn.
Created by circle on 10/30/2022 in #java-help
De-compiler assigning boolean value to int data type
No description
22 replies