How to add HTTP lifecycle middleware handler to spring?
Asked Answered
V

2

11

I have a CustomerController.java:

package com.satisfeet.http;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.satisfeet.core.model.Address;
import com.satisfeet.core.model.Customer;
import com.satisfeet.core.service.CustomerService;

@RestController
@RequestMapping("/customers")
public class CustomerController {

    @Autowired
    private CustomerService service;

    @RequestMapping(method = RequestMethod.GET)
    public Iterable<Customer> index() {
        return this.service.list();
    }

    @RequestMapping(method = RequestMethod.POST)
    public Customer create(@RequestBody Customer customer) {
        this.service.create(customer);

        return customer;
    }

    @RequestMapping(method = RequestMethod.GET, value = "/{id}")
    public Customer show(@PathVariable Integer id) {
        return this.service.show(id);
    }

    @RequestMapping(method = RequestMethod.PUT, value = "/{id}")
    public void update(@PathVariable Integer id, @RequestBody Customer customer) {
        this.service.update(id, customer);
    }

    @RequestMapping(method = RequestMethod.DELETE, value = "/{id}")
    public void destroy(@PathVariable Integer id) {
        this.service.delete(id);
    }

}

and a ExceptionController.java:

package com.satisfeet.http;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;

import com.satisfeet.core.exception.NotFoundException;

@ControllerAdvice
public class ExceptionController {

    @ExceptionHandler(NotFoundException.class)
    public ResponseEntity notFoundError() {
        return new ResponseEntity(HttpStatus.NOT_FOUND);
    }

}

I would now like to add some sort of HTTP request-response middleware which is executed before the response is written and write the HTTP status code in json:

HTTP/1.1 404 OK
Connection: close
Content-Type: application/json

{"error":"not found"}

I know how to convert a HttpStatus to a String but I do not know where I could do this globally with @ControllerAdvice.

So how do I register a global handler which has access to a response object?

Visitation answered 8/10, 2014 at 8:15 Comment(0)
S
23

i think what you are looking for is spring Interceptor .

please check this tutorial which is describing how to use interceptors .

and check spring documentation Intercepting requests with a HandlerInterceptor .

finally check this answer here

Hope that helps .

Strained answered 8/10, 2014 at 8:56 Comment(1)
The tutorial link is dead btw. use digitalocean.com/community/tutorials/… instead. However, the contents of the linked tutorial should not be used, as the HandlerInterceptorAdaptor is deprecatedGentile
I
0

To whom ever this may concern regarding the answer, the class HandlerInterceptorAdapter is now deprecated. You can implement the HandlerInterceptor which comes in the form of an interface instead. You can then implement preHandle, postHandle, or afterCompletion methods. See this question.

Iodism answered 30/3, 2022 at 10:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.