Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,156,433 members, 7,830,179 topics. Date: Thursday, 16 May 2024 at 05:23 PM

Please Help, @requestparam(required=false) Not Working - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Please Help, @requestparam(required=false) Not Working (383 Views)

Urgently Required: Programmer With Strong HTML, CSS And JAVA Background / What Are The Programming Languages Required To Make Interesting Cartoons? / True Or False? (2) (3) (4)

(1) (Reply) (Go Down)

Please Help, @requestparam(required=false) Not Working by InteliJ(f): 3:17am On Sep 12, 2023
Please help me look at this code. I've been trying to debug this for over 2 hours now. When I test the endpoint using postman, all parameters must be provided or else it gives a "Status 500, Internal Server Error" despite the fact that I've set required=false.

If I provide all parameters, it works tho.

The use case of this method won't require all parameters to be provided sometimes, so I'm worried about this.

Here's the code:

Controller class:
@PutMapping(path = "allproducts/{productId}"wink
public void updateProduct(@PathVariable("productId"wink Long productId,
@RequestParam(required = false) String name,
@RequestParam(required = false) String description,
@RequestParam(required = false) int quantity,
@RequestParam(required = false) Double weight,
@RequestParam(required = false) Double price) {
productService.updateProduct(productId, name, description, quantity, weight, price);

}

Service class:
@Transactional
public void updateStudent(Long studentId, String name, String email) {
Student student = studentRepository.findById(studentId)
.orElseThrow(() -> new IllegalStateException(
"student with id " + studentId + " does not exists"wink
);

if (name != null && name.length() > 0 && !Objects.equals(student.getName(), name)) {
student.setName(name);
}

if (email != null && email.length() > 0 && !Objects.equals(student.getEmail(), email)) {
Optional<Student> studentOptional = studentRepository.findStudentByEmail(email);
if (studentOptional.isPresent()) {
throw new IllegalStateException("Email is taken"wink;
}
student.setEmail(email);
}

}

Thank you for your time
Re: Please Help, @requestparam(required=false) Not Working by InteliJ(f): 3:19am On Sep 12, 2023
Please ignore the emoji in the code, I think that's how nairaland was built.
Re: Please Help, @requestparam(required=false) Not Working by richebony: 9:53pm On Sep 12, 2023
What is the class of the exception being thrown

And besides why dont you place all those request params asides the id inside the request body wrapped around a dto

1 Like

Re: Please Help, @requestparam(required=false) Not Working by InteliJ(f): 5:32pm On Sep 13, 2023
richebony:
What is the class of the exception being thrown

And besides why dont you place all those request params asides the id inside the request body wrapped around a dto
If I get your question right. The code is working, no exception is thrown in the terminal. The issue is that all parameters must be used. Or else, I get an internal server error in postman. Despite the fact that I set all requestparams required to false
Re: Please Help, @requestparam(required=false) Not Working by InteliJ(f): 5:35pm On Sep 13, 2023
richebony:
What is the class of the exception being thrown

And besides why dont you place all those request params asides the id inside the request body wrapped around a dto
In response to your second question:
Boss, no vex I never reach that level o, I'll have to research on how to do it. The code I wrote is what I saw in the tutorial used to learn. I intend to learn more and improve tho.

Thanks for taking the time to respond. I appreciate
Re: Please Help, @requestparam(required=false) Not Working by richebony: 10:56pm On Sep 13, 2023
InteliJ:

If I get your question right. The code is working, no exception is thrown in the terminal. The issue is that all parameters must be used. Or else, I get an internal server error in postman. Despite the fact that I set all requestparams required to false

yea, iits not supposed to act like that, that's why I asked what the class of the exception being thrown is , if it's an IllegalStateException then the error must have been thrown from the service layer and propagated to the controller class
Re: Please Help, @requestparam(required=false) Not Working by InteliJ(f): 5:15am On Sep 14, 2023
richebony:


yea, iits not supposed to act like that, that's why I asked what the class of the exception being thrown is , if it's an IllegalStateException then the error must have been thrown from the service layer and propagated to the controller class
When I run the application, in the ide terminal, I get the sql command with the parameters ending with question mark.

For example:
When I run the putmapping method, the parameters I didn't provide a value to, gets a question mark at the end. Like so:
Price?
Age?

I'm not on my system right now, when I charge it I'll take a picture of the terminal so you can understand what I'm saying better.
Re: Please Help, @requestparam(required=false) Not Working by InteliJ(f): 2:47pm On Sep 15, 2023
Sorry, I was saying bs all this while. I tried to run the program today and it threw an exception - NullPointerException.

I apologise for the confusion I caused you.

Below is the screenshot of the terminal.

Re: Please Help, @requestparam(required=false) Not Working by richebony: 4:57pm On Sep 15, 2023
From the stack trace your error seems to be coming from the productService class and the updateProduct method, seems you are trying to unbox a Double to its primitive value when it is apparently null ...

What is the code snippet in line 86 of your ProductService?

Apparently, you can make a null check before that line

if(weight!=null) {
//Perform business logic
}
Re: Please Help, @requestparam(required=false) Not Working by InteliJ(f): 7:36pm On Sep 15, 2023
richebony:
From the stack trace your error seems to be coming from the productService class and the updateProduct method, seems you are trying to unbox a Double to its primitive value when it is apparently null ...

What is the code snippet in line 86 of your ProductService?

Apparently, you can make a null check before that line

if(weight!=null) {
//Perform business logic
}
Done sir! Thank you very much for your time. I'll go ahead and look for how I can implement this:
place all those request params asides the id inside the request body wrapped around a dto

1 Like

(1) (Reply)

Pascal Founder Is Dead / As A Computer Science Student,where Can I Get This? / How Long Does It Take To Learn Python?

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 31
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.