[英]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.