Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Springboot How to Return a response after a POST

I would like to create a new customer and return a customer number once the customer is created. The customer number has to be an auto incremented unique number from 50000.

Thus far i have managed to created a customer but i am not sure how i should go about generating the customer number, save it to the database and show it to the user as a success message when a POST is triggered.

Below json is the desired response;

{
    "customerNumber": "50002",
    "statusMessage": "Customer Created Successfully",
} 

And the following snippet from controller and service;

UserService.java

public void createUser(User user) {
    if (user == null || user.getId() == null) {
        throw new ResourceNotFoundException("Empty", "Missing Data Exception");
    } else {
        userRepository.save(user);
    }
}

RegistrationController.java

@RequestMapping(method = RequestMethod.POST, value = "/users")
public void createUser(@RequestBody User user) {
    userService.createUser(user);
}
like image 315
kaddie Avatar asked Jan 10 '18 03:01

kaddie


2 Answers

Annotate the class containing createUser with @RestController, or add @ResponseBody on createUser method directly, and change its return type to Response;

@RestController
class Controller {

    @RequestMapping(method = RequestMethod.POST, value = "/users")
    public Response createUser(@RequestBody User user) {
        return userService.createUser(user);
    }
}

Assuming your createUser method in UserService will return a Response;

public Response createUser(User user) {
    if (user == null || user.getId() == null) {
        throw new ResourceNotFoundException("Empty", "Missing Data Exception");
    } else {
        User user = userRepository.save(user);
        return new Response(user.getId);
    }
}

Since fooRepository.save() always returns the successfully saved entity as long as it is the native save method from CrudRepository.

The id field will be present inside the resulting User entity from save, to return the type of response you want, you'd need to create such a class, and transfer the aforementioned id;

class Response {

    private String customerNumber;
    private String statusMessage;

    public Response(String id) {
        this.customerNumber = id;
        this.statusMessage = "Customer Created Successfully";
    }

    // getters, setters, etc
}
like image 144
buræquete Avatar answered Sep 28 '22 03:09

buræquete


For your controller to be able to return the id, the service layer should be able to provide that information.

In other words, a service or the repository should be able to inform you the id of the last added user. After getting that information, you can use it in the controller.

For example, some repository implementations set the id of the entity with the generated id after the saving (and commit of the transaction). If that was your case, you could do:

@RequestMapping(method = RequestMethod.POST, value = "/users")
@ResponseBody
public long createUser(@RequestBody User user){
     userService.createUser(user);
     return user.getId();
}

Notice the addition of the return clause, the returning type and the @ResponseBody annotation.

like image 31
acdcjunior Avatar answered Sep 28 '22 04:09

acdcjunior