Spring Rest and Jsonp

前端 未结 1 1036
旧时难觅i
旧时难觅i 2021-02-14 18:03

I am trying to get my Spring rest controller to return jsonp but I am having no joy

The exact same code works ok if I want to return json but I have a r

1条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-02-14 18:56

    As stated on the spring.io blog regarding the Spring 4.1 release:

    JSONP is now supported with Jackson. For response body methods declare an @ControllerAdvice as shown below. For View-based rendering simply configure the JSONP query parameter name(s) on MappingJackson2JsonView.

    @ControllerAdvice
    private static class JsonpAdvice extends AbstractJsonpResponseBodyAdvice {
    
        public JsonpAdvice() {
            super("callback");
        }
    
    }
    

    [...] In 4.1 an @ControllerAdvice can also implement ResponseBodyAdvice in which case it will be called after the controller method returns but before the response is written and therefore committed. This has a number of useful applications with @JsonView the JSONP already serving as two examples built on it.

    Javadoc taken from MappingJackson2JsonView:

    Set JSONP request parameter names. Each time a request has one of those parameters, the resulting JSON will be wrapped into a function named as specified by the JSONP request parameter value. The parameter names configured by default are "jsonp" and "callback".

    You don't need to implement this stuff by yourself. Just reuse the bits from the Spring Framework.

    Spring Boot example

    Following simple Spring Boot application demonstrates use of build in JSONP support in Spring MVC 4.1. Example requires at least Spring Boot 1.2.0.RC1.

    import com.fasterxml.jackson.annotation.JsonAutoDetect;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.boot.autoconfigure.web.HttpMessageConverters;
    import org.springframework.context.annotation.Bean;
    import org.springframework.http.converter.HttpMessageConverter;
    import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
    import org.springframework.web.bind.annotation.ControllerAdvice;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    import org.springframework.web.servlet.mvc.method.annotation.AbstractJsonpResponseBodyAdvice;
    
    import java.util.Collections;
    
    import static com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility.ANY;
    import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;
    
    @RestController
    @SpringBootApplication
    class Application {
    
        @JsonAutoDetect(fieldVisibility = ANY)
        static class MyBean {
            String attr = "demo";
        }
    
        @ControllerAdvice
        static class JsonpAdvice extends AbstractJsonpResponseBodyAdvice {
            public JsonpAdvice() {
                super("callback");
            }
        }
    
        @Bean
        public HttpMessageConverters customConverters() {
            return new HttpMessageConverters(false, Collections. >singleton(new MappingJackson2HttpMessageConverter()));
        }
    
        @RequestMapping
        MyBean demo() {
            return new MyBean();
        }
    
        @RequestMapping(produces = APPLICATION_JSON_VALUE)
        String demo2() {
            return "demo2";
        }
    
        public static void main(String[] args) {
            SpringApplication.run(Application.class, args);
        }
    }
    

    URL http://localhost:8080/demo?callback=test converts a POJO into a JSONP response:

    test({"attr":"demo"});
    

    URL http://localhost:8080/demo2?callback=test converts a String into a JSONP response:

    test("demo2");
    

    0 讨论(0)
提交回复
热议问题