How to simulate a CANCEL action in a RESTful way?

We are currently collecting small services from our monoliths. Our domain is very similar to the ticketing system. We decided to start with the domain cancellation process.

Our cancellation service has a simple cancellation endpoint that accepts a ticket identifier. Inside, we extract the identifier, perform some operations related to its cancellation, and update the state of the object in the repository. From the store’s point of view, the only difference between a canceled ticket and a live ticket is a few properties.

From what I read, PATCH seems to be the correct verb to use in this case, since I am updating only a simple property in the resource.

PATCH /api/tickets/{id}
Payload {isCancelled: true}

But isCancelled is not an actual property in essence. Is it fair to send properties that are not part of the object to the payload, or should I think of some other form of modeling for this request? I would not want to send the whole entity as part of the payload, since it is large.

I considered creating a new CanceledTickets resource, but in our domain we will never need a GET for canceled tickets. Therefore, stay away from the need to create a new resource.

+7
source share
4 answers

, RESTful. , PATCH isCancelled DELETE, , . RESTful.

.

CancelledTickets, GET .

DELETE. . , isCancelled. .

+5

GET .

,

PUT /api/tickets/{id}/actions/cancel

. PUT, .

, .

+2

REST - - . , , REST.

, -? , , , , URI . , URI PATCH DELETE . , .

, REST. , , , . - reserve new tickets, edit reservation cancel reservation. URL-, , . URI . ( ), , . , , HTTP OPTIONS URI . , , - .

, API . , , URI , URI . - URI, , URI, , URI, . , , , URI. , -. , 2008 :

API- REST , , / . ()

HTTP /, . DELETE RFC 7231 , URI , , . , , DELETE , URI DELETE. , , DELETE.

, , PATCH . - state=canceled, , , PATCH - , ( ) . JSON Patch , . atomicy PATCH. , .

PUT . PUT , URI, , . , , , .. .

, POST swiss-Army-knife HTTP. , . , . HTTP

POST , , , .

, , " ". . -, .

GET , / -. GET , . , , , , . -, . URI GET . , , Google ( ) , ?

, HTTP- , . DELETE , , , , URI , ( , URI). , , , , PATCH POST .

+2

To simulate the CANCEL Restful action: suppose we need to delete a note in the database by specifying noteId (Note ID), and Note is pojo

1] On the controller:

 @DeleteMapping(value="/delete/{noteId}")
     public ResponseEntity<Note> deleteNote( @PathVariable Long noteId)
     {
        noteServiceImpl.deleteNote(noteId);
        return ResponseEntity.ok().build();
     }

2] Service level:

@Service
public class NoteServiceImpl {

@Autowired
private NotesRepository notesDao;

public void deleteNote(Long id) {
    notesDao.delete(id);
}

}

3] Repository layer:

 @Repository
public interface NotesRepository extends CrudRepository<Note, Long> {
 }

and at 4] postman: http: // localhost: 8080 / delete / 1

enter image description here

Thus, we removed the note Id 1 from the database using the CANCEL action

0
source

Source: https://habr.com/ru/post/1671145/


All Articles