简体   繁体   中英

returning a created ID from Oracle database using java Spring

For example I can use the POST method to send some JSON to the web service and then create an object. Is it best practice to then set the HTTP status to created (201) or ok (200) and simply return a message such as "New Employee added", or return the object UUID which is auto generated during save?

If I got correctly. You can create method that can return, for example ResponseEntity<?>, where? - entity class. And, depends on how you save you entity to db. If you use JPARepository, just use return repository.save(entity) in the controller.

If you return a 201 - Created you should specify the URI of the newly created resource, this means providing a Location header with the full URI where the new resource can be retrieved from.

Returning a 200 OK with the resource created in the body is also fine.

Both alternatives are trying to make sure the client making the POST knows where to find the resource they just created.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM