EzyHTTP Response Types

Updated at 1685686388000
EzyHTTP supports 4 response types:

Response TypeDescription1ResponseEntityWraps a response status code and a response data2ObjectReturns a response data with status code 2003ViewReturns a view template to render html4RedirectReturns 302 status code

1. ResponseEntity

If you want to return a json data or not found status code for Rest API to client, you can use ResponseEntity like this:

    @DoGet("/books/{bookId}")
    public ResponseEntity getBook(@PathVariable Long bookId) {
        final BookData bookData = bookService.getBook(bookId);
        if (bookData == null) {
            return ResponseEntity.notFound(
                Collections.singletonMap("book", "notFound")
            );
        }
        return ResponseEntity.ok(bookData);
    }

Or if you want to return conflict response code, you can do like this:

    @DoPost("/book/add")
    public ResponseEntity addBook(@RequestBody AddBookRequest request) {
        bookValidator.validate(request);
        BookData currentBook = bookService.getBook(request.getBookName());
        if (currentBook != null) {
            ResponseEntity
                .status(StatusCodes.CONFLICT)
                .body(Collections.singletonMap("book", "existed"));
        }
        bookService.addBook(requestToDataConverter.toData(request));
        return ResponseEntity.noContent();
    }

2. Return an Object

If you want to return a json data for Rest API to client, and you will process exceptions in a GlobalExceptionHandler you can use do like this:

    @DoGet("/books/{bookId}")
    public BookResponse getBook(@PathVariable Long bookId) {
        final BookData bookData = bookService.getBook(bookId);
        return dataToResponseConverter.toResponse(bookData);
    }

3. View

Currently, EzyHTTP support you return html page with thymeleaf, you can return a view like this:

    @DoGet("/user/update")
    public View userUpdateGet(@UserId long userId) {
        final User user = userService.getUserById(userId);
        return View.builder()
                .addVariable("user", user)
                .addVariable("accountType", user.getAccountType().toString())
                .template("user-update")
                .build();
    }

4. Redirect

In a view controller, when handle POST, PUT or DELETE requests, you will can not return a view, so, you will need return a redirect like this:

    @DoPost("/login")
    public Redirect loginGet(
        @RequestBody LoginRequest request
    ) {
        final User user = authenticationService.authen(
            request.getEmail(),
            request.getPassword()
        );
        return Redirect.builder()
           .addCookie("accessToken", user.getAccessToken(), "/")
           .addAttribute("username", user.getUsername())
           .uri("/home")
           .build();
    }

And in the home controller, you can get the username attribute like this:

    @DoGet("/home")
    public Object home(
        RequestArguments arguments,
        @UserId long userId
    ) {
        String username = arguments.getRedirectionAttribute("username");
        // implementation
    }

5. Next

You can take a look List of EzyHTTP's annotations.