katharsis-project / katharsis-framework

Katharsis adds powerful layer for RESTful endpoints providing implementenation of JSON:API standard
http://katharsis.io
Apache License 2.0
133 stars 65 forks source link

SpringBoot: use interceptors with Katharsis? #420

Open adhamhf opened 7 years ago

adhamhf commented 7 years ago

Is it possible to use Spring interceptors with Katharsis? It seems unlikely since Katharsis ends up bypassing Spring MVC which is what provides interceptors, but I thought I would ask.

jianglibo commented 7 years ago

Katharsis has own interceptors too. For your question, Spring interceptors are sitting in spring dispatch servlet. when you configuring Katharsis as a filter, they are in the different branches, so the answer is no. But it's easy to setup Katharsis as a spring controller and make interceptor work. In most case, it's not necessary. Katharsis is focused on Jsonapi, it solves the problem about jsonapi and does it well.

adhamhf commented 7 years ago

@jianglibo do you have an example of how to setup Katharsis as a Spring Controller and/or how to use Katharsis interceptors?

jianglibo commented 7 years ago

Reference the KatharsisFilterV2 class and look at the doFilter code, copy all code to your new created class, for example, named "KatharsisProcessor". Because Katharsis's code is clean and tidy, It's a simple job. after that, create a controller like below.

@Controller
public class KatharsisController {

    @Autowired
    private KatharsisProcessor processor;

    @RequestMapping(value="/jsonapi/**")
    public void process(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        try {
            processor.process(request, response);
        } catch (Exception e) {
            e.printStackTrace();
            throw(e);
        }
    }
}
chb0github commented 7 years ago

Hmm, That's interesting.

adhamhf commented 7 years ago

@jianglibo this is great. It's not quite as easy as you suggest, but generally it looks to work. It also solves my other issue because I can have more specific paths for endpoints I want to bypass katharsis. This might be a good example.

Here's the documentation on @RequestMapping and regexes. http://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html#mvc-ann-requestmapping-uri-templates-regex