Spring interception

First, I am a new to Spring and this is my first attempt to write a REST based application using Spring.

I am planning to use Json both request parameters and in responses. This brings me to two questions.

  1. Is there a way to globally set produces="application/json" as default for all my mvc controllers responses.

  2. If anyone sends a request and expects a result in other format than application/json, I would like to return a error message. I guess this i a interceptors responsibility but understand how to set up the check.

Answer:1

While there's no way to set produces globally and, as Tomasz suggested it's usually easier to leave content negotiation to spring, it's also quite easy to implement the functionality as an interceptor.

Basically you need to return 415 status code on any request that doesn't have Accept header with application/json as value.

So first implement the interceptor:

public class WrongAcceptHeaderInterceptor extends HandlerInterceptorAdapter {

    public boolean preHandle(
            HttpServletRequest request,
            HttpServletResponse response,
            Object handler) throws Exception {
        if (!"application/json".equals(request.getHeader("Accept"))) {
            //you can change or omit the message 
            response.sendError(SC_UNSUPPORTED_MEDIA_TYPE, "Your error message"); 
            // return false to skip further processing
            return false;
        }
    }
}

and then register it in your context:

<mvc:interceptors>
    <bean class="foo.bar.baz.WrongAcceptHeaderInterceptor" />
</mvc:interceptors>
Answer:2

I am doing project using Lucen library in here I need to build query dynamically using Json object. so in here I use jettison library. as an example my json like this {"OR":{"OR": {"fildKey1": "...

Internal frame Decoration I would like to know, is there any way to set my internal frame decoration as normal window decoration instead look like the image inside the link above? Thank you very much....

Hola, tengo un problema con mi código, creé una cuadrícula con un complemento como este: http://www.sencha.com/examples-2/#gridplugins (la segunda cuadrícula) bueno, ahora necesito saber qué elementos se seleccionan cuando yo ...

Tengo debajo de la consulta SQL. ¿Cuál es el criterio equivalente o consulta HQL? seleccione max (id) de (seleccione max (id) id de TableA union seleccione max (id) de TableB) ¡Gracias!