I am getting 404 error code while performing post request , but the data is successfully getting inserted into the database on spring boot appilcation

pankaj yadav

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()
}
Itamarled

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Why am I getting an 404 Error from my POST request?

I am getting a 404 Error on API Call in Spring Boot

Why am I getting a 500 error even though post request's being sent to DB successfully?

Why am I getting a 404 error with my Laravel AJAX POST request?

Why am I getting a 404 status error on my PUT request?

I am getting 404 not found error while the route exisits

performing MockMvc : getting error while performing mockMvc on post api

why am i getting this error of Database Error: Permission Denied while retrieving data

I'm getting error 404 while trying to access my spring boot app on Amazon Elastic Bean Stalk

Why am I getting a NG0901 error when I am not performing any data change?

Why I am getting error not found error in Spring boot application

while installing request module i am getting an error

Why am I getting "An error occured while processing your request"?

I am not getting data at server side request.post()

I am getting the date instead of date and time data in a POST request

I am getting following error while connecting to my database

Why am I getting syntax error while trying to update database?

i am getting an error while writing an mysql query for deletin duplicate data in database

I am getting an authentication error while updating data in Azure table storage using SharedKeyLite in NodeJS code

I'm getting AXIOS error code 404 when trying to post data

I am getting error while fetching data in react-native

Why am I getting error for access restriction when performing code for forwarding emails?

How to insert data in database in android I am getting some error?

Data is not getting inserted in Spring boot + Spring data JPA?

I'm getting a ERROR Request failed with status code 404 AxiosError: Request failed with status code 404

I am performing wordcount in Mapreduce and getting the following error in -copyToLocal

Node, express, Monk, mongodb: 400 bad request error on post request. Why am I getting this error?

I am getting an error while installing pyinstaller

I am getting an error while producing objects