简体   繁体   English

我在执行发布请求时收到 404 错误代码,但数据已成功插入 spring 启动应用程序上的数据库中

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

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.当我从 postman 执行发布/删除操作时,我收到 404 错误代码,但数据已成功插入或删除到我的数据库中。 I am unable to understand why my code is behaving like this.我无法理解为什么我的代码会这样。 Sharing screenshot database , api response分享截图数据库api响应

Controller class 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服务 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 build.gradle文件

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

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成功请求的另一个选项是使用此注释: @ResponseStatus

you can learn more about this here: https://www.baeldung.com/spring-response-status你可以在这里了解更多信息: https://www.baeldung.com/spring-response-status

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我在Spring Boot中遇到API调用404错误 - I am getting a 404 Error on API Call in Spring Boot 我没有收到任何错误,但没有任何内容插入到我的数据库中 - I am not getting any error but nothing is getting inserted into my database 我有 Spring Boot REST API 应用程序,当多个用户调用此 API 时,它具有 POST 调用我面临数据不一致 - I am having Spring Boot REST API appilcation which has POST call when multiple users are calling this API I am facing data inconsistency 为什么我的 Spring Boot Web 应用程序会出现 404 错误? - Why am I getting a 404 error in my Spring Boot Web app? 当我尝试使用 Spring Boot 将产品添加到购物车时出现错误 - I am getting an error while i am trying to add a product to the cart using Spring boot 我正在升级到 spring 启动 2.5.12,并在下面收到此错误 - I am upgrading to spring boot 2.5.12, and am getting this error below 在 Spring Boot 项目中,启用“HTTPS”时不支持 POST 调用,因为我收到错误 405 - In Spring Boot Project, POST Calls are not supported when 'HTTPS' is enabled, as I am getting Error 405 我在使用 spring boot 版本 2.2.0 的 spring 批处理中遇到错误 - I am getting error in spring batch with spring boot version 2.2.0 spring和angulaJS(我收到类似HTTP Status 404的错误) - spring and angulaJS(i am getting error like HTTP Status 404 ) Spring MVC,为什么会出现 404 错误? - Spring MVC, Why am I getting a 404 error?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM