When I am performing the post/Delete operation from postman, I am getting 404 error code ,but the data is successfully getting inserted or deleted into my database. I am unable to understand why my code is behaving like this. Sharing screenshot database, api response
Controller class
@Controller
public class UserController {
@Autowired
private UserService userService;
@PostMapping("/user/add")
public void addNewUser(@RequestBody User user) throws BLException {
userService.addNewUser(user);
}
@DeleteMapping("/user/delete/{id}")
public void deleteById(@PathVariable int id){
userService.deleteById(id);
}
@DeleteMapping("/deleteAll")
public void deleteAll(){
userService.deleteAll();
}
}
Service class
@Service
public class UserService {
@Autowired
public UserRepository userRepository;
public void addNewUser(User user) throws BLException {
try {
ValidateMobileNumber.validate(user.getPhone());
userRepository.save(user);
}
catch (BLException e){
System.out.println(e.getMessage());
}
}
public void deleteById(int id){
userRepository.deleteById(id);
}
public void deleteAll(){
userRepository.deleteAll();
}
}
build.gradle file
plugins {
id 'java'
}
group 'org.example'
version '1.0-SNAPSHOT'
repositories {
mavenCentral()
}
dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.1'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.1'
// https://mvnrepository.com/artifact/org.springframework/spring-web
implementation group: 'org.springframework', name: 'spring-web', version: '5.3.16'
// https://mvnrepository.com/artifact/io.springfox/springfox-swagger2
implementation group: 'io.springfox', name: 'springfox-swagger2', version: '2.9.2'
// https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web
implementation group: 'org.springframework.boot', name: 'spring-boot-starter-web', version: '2.6.3'
// https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-jdbc
// https://mvnrepository.com/artifact/org.projectlombok/lombok
compileOnly group: 'org.projectlombok', name: 'lombok', version: '1.18.22'
// https://mvnrepository.com/artifact/org.springframework.data/spring-data-commons
implementation group: 'org.springframework.data', name: 'spring-data-commons', version: '2.6.1'
// https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-devtools
implementation group: 'org.springframework.boot', name: 'spring-boot-devtools', version: '2.6.3'
// https://mvnrepository.com/artifact/org.aspectj/aspectjweaver
implementation group: 'org.aspectj', name: 'aspectjweaver', version: '1.9.8'
// https://mvnrepository.com/artifact/org.aspectj/aspectjrt
implementation group: 'org.aspectj', name: 'aspectjrt', version: '1.9.8'
// https://mvnrepository.com/artifact/aopalliance/aopalliance
implementation group: 'aopalliance', name: 'aopalliance', version: '1.0'
// https://mvnrepository.com/artifact/cglib/cglib
implementation group: 'cglib', name: 'cglib', version: '3.3.0'
// https://mvnrepository.com/artifact/org.ow2.asm/asm
implementation group: 'org.ow2.asm', name: 'asm', version: '9.2'
// https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind
implementation group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.0.1'
// https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-jpa
implementation group: 'org.springframework.boot', name: 'spring-boot-starter-data-jpa', version: '2.7.2'
// https://mvnrepository.com/artifact/mysql/mysql-connector-java
implementation group: 'mysql', name: 'mysql-connector-java', version: '8.0.30'
// https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-jdbc
implementation group: 'org.springframework.boot', name: 'spring-boot-starter-jdbc', version: '2.7.2'
implementation group: 'javax.xml.bind', name: 'jaxb-api', version: '2.2.4'
}
test {
useJUnitPlatform()
}
CodePudding user response:
on success of your action you can change the response result like this:
@PostMapping("CreateUserWithEmailAndPassword")
public void createUserWithEmailAndPassword(@RequestBody User newUser, @RequestParam String password, HttpServletResponse response) throws FirebaseAuthException {
userService.createUserWithEmailAndPassword(newUser, password);
response.setStatus(HttpServletResponse.SC_OK);
}
you get the response parameter for "free" whenever you send a request.
if your request fails throw an error and and catch it in your exception handler, there assign the appropriate status code according to the reason of failure.
Another option for successful requests is to use this annotation: @ResponseStatus
you can learn more about this here: https://www.baeldung.com/spring-response-status