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:

    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:

    public ResponseEntity addBook(@RequestBody AddBookRequest request) {
        BookData currentBook = bookService.getBook(request.getBookName());
        if (currentBook != null) {
                .body(Collections.singletonMap("book", "existed"));
        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:

    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:

    public View userUpdateGet(@UserId long userId) {
        final User user = userService.getUserById(userId);
        return View.builder()
                .addVariable("user", user)
                .addVariable("accountType", user.getAccountType().toString())

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:

    public Redirect loginGet(
        @RequestBody LoginRequest request
    ) {
        final User user = authenticationService.authen(
        return Redirect.builder()
           .addCookie("accessToken", user.getAccessToken(), "/")
           .addAttribute("username", user.getUsername())

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

    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.