guilhermedias / generator-java-service

Java backend service generator
11 stars 1 forks source link

Add Rest Generator #21

Open willmenn opened 7 years ago

willmenn commented 7 years ago

Why this change is needed? When someone need to prototype a project, sometimes it needs a rest endpoint to perform some actions. This pr intent to create a template for the developer to use.

How this commit address this issue? It create a new generator inside macchiato. To run yo macchiato:rest.


screen shot 2017-07-26 at 23 23 01

It will create a similar Rest Controller:

@RestController
public class UserRestController {

    @GetMapping
    @ResponseStatus(OK)
    public User get(@RequestParam("id") String id) {
        //TODO: fetch the data from service layer.
        return User.from(id);
    }

    @PostMapping(consumes = APPLICATION_JSON_VALUE)
    @ResponseStatus(CREATED)
    public User create(@RequestBody User resource) {
        //TODO: goes to service layer to create the entity.
        return User.createSelfLink(resource);
    }

    @DeleteMapping
    @ResponseStatus(NO_CONTENT)
    public void delete(@RequestParam("id") String id) {
        //TODO: Goes to service layer and delete the entity.
    }

    @AllArgsConstructor
    @Getter
    private static class User extends ResourceSupport {
        private final UUID resourceId;

        private static User from(String id) {
            User resource = new User(UUID.fromString(id));
            resource.add(linkTo(methodOn(UserRestController.class)
                    .get(id))
                    .withSelfRel());
            return resource;
        }

        private static User createSelfLink(User resource) {
            return from(resource.getResourceId().toString());
        }
    }
}