0

I am developing simple rest api with spring boot. I create users via POST method. And deletes via DELETE. But when i use DELETE with json server returns Bad Request.

creating user:

ubuntu@ubuntu-pc:~$ curl -X POST -H "Content-type: application/json" -d '{"name": "developer", "email": "[email protected]"}' http://localhost:8080/add-user "OK" 

getting users:

ubuntu@ubuntu-pc:~$ curl http://localhost:8080 [{"id":"ff80818176c9b9720176c9bdfd0c0002","name":"developer","email":"[email protected]"}] 

deleting user using json:

ubuntu@ubuntu-pc:~$ curl -X DELETE -H "Content-type: application/json" -d '{"id": "ff80818176c9b9720176c9bdfd0c0002"}' http://localhost:8080/del-id {"timestamp":"2021-01-03T19:47:15.433+00:00","status":400,"error":"Bad Request","message":"","path":"/del-id"} 

deleting user using html queries:

ubuntu@ubuntu-pc:~$ curl -X DELETE http://localhost:8080/del-id?id=ff80818176c9b9720176c9bdfd0c0002 "OK" ubuntu@ubuntu-pc:~$ curl http://localhost:8080 [] 

UserRepository.java

public interface UserRepository extends CrudRepository<UserRecord, String> { } 

UserService.java

@Service public class UserService { @Autowired private UserRepository userRepository; public List<UserRecord> getAllUsers() { List<UserRecord> userRecords = new ArrayList<>(); userRepository.findAll().forEach(userRecords::add); return userRecords; } public void addUser(UserRecord user) { userRepository.save(user); } public void deleteUser(String id) { userRepository.deleteById(id); } } 

UserController.java

@RestController public class UserController { @Autowired private UserService userService; @RequestMapping("/") public List<UserRecord> getAllUser() { return userService.getAllUsers(); } @RequestMapping(value="/add-user", method=RequestMethod.POST) public HttpStatus addUser(@RequestBody UserRecord userRecord) { userService.addUser(userRecord); return HttpStatus.OK; } @RequestMapping(value="/del-id", method=RequestMethod.DELETE) public HttpStatus deleteUser(@RequestParam("id") String id) { userService.deleteUser(id); return HttpStatus.OK; } } 

jvm log:

2021-01-03 22:47:15.429 WARN 30785 --- [nio-8080-exec-8] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.web.bind.MissingServletRequestParameterException: Required String parameter 'id' is not present] 

What am i wrong?

1
  • As you can see, the DELETE endpoint only needs a parameter (id) in the request - not a JSON object. So, this http://localhost:8080/del-id?id=ff80818176c9b9720176c9bdfd0c0002 is the correct request - if you add it to the body in JSON, then you will get a Bad Request (as the parameter is not where it is supposed to be) Commented Jan 3, 2021 at 20:05

1 Answer 1

3

Spring @RequestParam Annotation

The @RequestParam Annotation is designed to derive the value from the URL. When you pass in the id with the requestBody, it's not being populated into the deleteUser function.

Either change the method to also use the @RequestBody annotation or pass in the id through the path param like you do in the html query.

Sign up to request clarification or add additional context in comments.

2 Comments

I updated code by taking attention your answer. It's deletes user but returns "OK"{"timestamp":"2021-01-04T15:36:41.328+00:00","status":500,"error":"Internal Server Error","message":"","path":"/del-id"}. Why returning INTERNAL_SERVER_ERROR after "OK" message?
Hmm. So you're returning HttpStatus.OK if the method completes successfully, which is where I assume the "OK" comes from, but there must be some error being buried inside the repository method call? Maybe debug from inside the deleteUser method and see if you're hitting some kind of exception?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.