0

I've a scenario where a post request from first microservice creates new user and creates a wallet for newly created user from different microservice.

I'm returning HTTP status 201 when both user and wallet created. I'm bit confused what status should I return if user is created but wallet isn't.

I came across some articles and found two relevant to my confusion but are contradictory on returning HTTP status 207 due to its limitation related to WebDAV.

  1. Is Http response 207 MULTI-STATUS appropriate for multi task operations?
  2. REST API response in partial success

refer my code -

    @PostMapping("/register")
public ResponseEntity<User> saveUser(@RequestBody User user) {
    user.setUserId(sequenceGeneratorService.generateSequence(User.SEQUENCE_NAME));
    user.getRoles().forEach(role -> role.setRoleId(sequenceGeneratorService.generateSequence(Role.SEQUENCE_NAME)));

    User savedUser = userService.saveUser(user);
    ResponseEntity<Wallet> createdWallet = createUserWallet(savedUser);
    if (createdWallet.getStatusCode().is2xxSuccessful()) {
        savedUser.setWallet(createdWallet.getBody());
        return new ResponseEntity<User>(savedUser, HttpStatus.CREATED);
    } else {// here is the confusion

        return new ResponseEntity<User>(savedUser, HttpStatus.MULTI_STATUS);
    }
}

private ResponseEntity<Wallet> createUserWallet(User savedUser) {
    Wallet userWallet = Wallet.builder()
            .walletId(sequenceGeneratorService.generateSequence(Wallet.SEQUENCE_NAME))
            .userId(savedUser.getUserId())
            .balance(BigDecimal.ZERO).build();
    return walletServiceProxy.createWallet(userWallet);
}

May I know which status should I return here?

1 Answers1

0

I'm returning HTTP status 201 when both user and wallet created. I'm bit confused what status should I return if user is created but wallet isn't.

HTTP status codes are metadata of the transfer-of-documents-over-a-network domain (see Webber 2011); they are there so that general purpose HTTP components can correctly interpret the response, and do intelligent things (like invalidating previously cached responses, when appropriate).

Your HTTP API is a facade: a costume that your implementation wears that makes it look like an HTTP aware document store. (The fact that your implementation doesn't have "documents" is an implementation detail, hidden behind this facade.)

The responses you send should be understood in this same way - you are telling the HTTP client (and also any intermediary components who can read the response metadata) how your (imaginary) web page reacted to the request that was sent.

Was the message processed successfully? Did it create a new (imaginary) web page with its own identifier, that clients can send messages to? Then you should normally be sending back a 201 Created, even if the implementation didn't achieve that outcome via the "happy path".

On the other hand, if you want general purpose components to understand that request processing failed, you send a 4XX Client Error or a 5XX Server Error, as appropriate.

(You probably shouldn't be using 207 MultiStatus unless you are deliberately doing WebDav things and are expecting requests from WebDav aware components; it doesn't achieve anything useful unless the client implementation knows how to handle multistatus XML documents).


Reminder: the part of an HTTP response where you describe in detail what happened and how the end consumer can respond to it is the body of the HTTP response.

201 Created
Location: /users/12345
Content-Type: text/plain

Hey, we created the new user you asked us to.  Isn't that great?
You can review the details of the user at: /users/12345

But, we weren't able to create a wallet for the user.  If that's
kind of important to you, could you fill in this form and send it
to us: /lost-wallets#form ?

Thanks, and have a great day
VoiceOfUnreason
  • 52,766
  • 5
  • 49
  • 91
  • Well, now as I understood that I've to return dedicated HTTP responses for each operation, I've to work on how I can send multiple response codes from spring boot rest api(which I was trying initially i.e. before asking this question). Because consumer of this service must receive an information whether wallet is created or not. If you're aware about that please let me know. – Akshay Sumbe Jan 26 '23 at 05:42