Skip to content

Spring project. Learning about DTOs and using Annotations to implement the POST and GET methods

Notifications You must be signed in to change notification settings

Lucas-zz/CarsAPI

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

About

Java studies.

Implementing the POST method with Spring using its corresponding Annotation @PostMapping as well as learning more about DTOs, how to create, how to use and why it's used.

@RestController
@RequestMapping("/api/cars")
public class CarsController {
    String cars;

    @GetMapping
    ResponseEntity<String> getCars() {
        if (cars == null) {
            return ResponseEntity.status(HttpStatus.NOT_FOUND)
                    .body("The list of cars is empty, submit a car first.");
        }

        return ResponseEntity.status(HttpStatus.OK)
                .body("The list of cars: \n" + cars);
    }

    @PostMapping
    public void createCar(@RequestBody String req) {
        cars = req;

        System.out.println(req);
    }
}

HTTP Responses and Status

First GET when no cars were posted (Response 404 - Not Found)

First POST (Response 200 - OK)

Second GET when the cars were posted (Response 200 - OK)

Getting Started

Reference Documentation

For further reference, please consider the following sections:

Guides

The following guides illustrate how to use some features concretely:

About

Spring project. Learning about DTOs and using Annotations to implement the POST and GET methods

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages