Search in sources :

Example 1 with ResponseJSONP

use of com.alibaba.fastjson.support.spring.annotation.ResponseJSONP in project uavstack by uavorg.

the class JSONPResponseBodyAdvice method beforeBodyWrite.

public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType, Class<? extends HttpMessageConverter<?>> selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) {
    ResponseJSONP responseJsonp = returnType.getMethodAnnotation(ResponseJSONP.class);
    if (responseJsonp == null) {
        responseJsonp = returnType.getContainingClass().getAnnotation(ResponseJSONP.class);
    }
    HttpServletRequest servletRequest = ((ServletServerHttpRequest) request).getServletRequest();
    String callbackMethodName = servletRequest.getParameter(responseJsonp.callback());
    if (!IOUtils.isValidJsonpQueryParam(callbackMethodName)) {
        if (logger.isDebugEnabled()) {
            logger.debug("Invalid jsonp parameter value:" + callbackMethodName);
        }
        callbackMethodName = null;
    }
    JSONPObject jsonpObject = new JSONPObject(callbackMethodName);
    jsonpObject.addParameter(body);
    beforeBodyWriteInternal(jsonpObject, selectedContentType, returnType, request, response);
    return jsonpObject;
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletServerHttpRequest(org.springframework.http.server.ServletServerHttpRequest) JSONPObject(com.alibaba.fastjson.JSONPObject) ResponseJSONP(com.alibaba.fastjson.support.spring.annotation.ResponseJSONP)

Example 2 with ResponseJSONP

use of com.alibaba.fastjson.support.spring.annotation.ResponseJSONP in project fastjson by alibaba.

the class JSONPResponseBodyAdvice method beforeBodyWrite.

public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType, Class<? extends HttpMessageConverter<?>> selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) {
    ResponseJSONP responseJsonp = returnType.getMethodAnnotation(ResponseJSONP.class);
    if (responseJsonp == null) {
        responseJsonp = returnType.getContainingClass().getAnnotation(ResponseJSONP.class);
    }
    HttpServletRequest servletRequest = ((ServletServerHttpRequest) request).getServletRequest();
    String callbackMethodName = servletRequest.getParameter(responseJsonp.callback());
    if (!IOUtils.isValidJsonpQueryParam(callbackMethodName)) {
        if (logger.isDebugEnabled()) {
            logger.debug("Invalid jsonp parameter value:" + callbackMethodName);
        }
        callbackMethodName = null;
    }
    JSONPObject jsonpObject = new JSONPObject(callbackMethodName);
    jsonpObject.addParameter(body);
    beforeBodyWriteInternal(jsonpObject, selectedContentType, returnType, request, response);
    return jsonpObject;
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletServerHttpRequest(org.springframework.http.server.ServletServerHttpRequest) JSONPObject(com.alibaba.fastjson.JSONPObject) ResponseJSONP(com.alibaba.fastjson.support.spring.annotation.ResponseJSONP)

Example 3 with ResponseJSONP

use of com.alibaba.fastjson.support.spring.annotation.ResponseJSONP in project fastjson by alibaba.

the class FastJsonViewAndJSONPControllerTest method test3.

@ResponseJSONP
@RequestMapping("test3")
@FastJsonView(include = { @FastJsonFilter(clazz = Company.class, props = { "id", "name", "rootDepartment" }), @FastJsonFilter(clazz = Department.class, props = { "description" }) })
public Company test3() {
    Company company = new Company();
    company.setId(100L);
    company.setName("测试");
    company.setDescription("fastjsonview注解测试");
    company.setStock("haha");
    Department department = new Department();
    department.setName("部门1");
    department.setDescription("部门1描述");
    department.setId(1L);
    company.setRootDepartment(department);
    return company;
}
Also used : Company(com.alibaba.json.test.entity.Company) Department(com.alibaba.json.test.entity.Department) FastJsonView(com.alibaba.fastjson.support.spring.annotation.FastJsonView) ResponseJSONP(com.alibaba.fastjson.support.spring.annotation.ResponseJSONP) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 4 with ResponseJSONP

use of com.alibaba.fastjson.support.spring.annotation.ResponseJSONP in project fastjson by alibaba.

the class FastJsonViewAndJSONPControllerTest method test4.

@ResponseJSONP
@RequestMapping("test4")
@FastJsonView(include = { @FastJsonFilter(clazz = Company.class, props = { "id", "name", "rootDepartment" }) }, exclude = { @FastJsonFilter(clazz = Department.class, props = { "description", "memebers", "children" }) })
public Company test4() {
    Company company = new Company();
    company.setId(100L);
    company.setName("测试");
    company.setDescription("fastjsonview注解测试");
    company.setStock("haha");
    Department department = new Department();
    department.setName("部门1");
    department.setDescription("部门1描述");
    department.setId(1L);
    company.setRootDepartment(department);
    return company;
}
Also used : Company(com.alibaba.json.test.entity.Company) Department(com.alibaba.json.test.entity.Department) FastJsonView(com.alibaba.fastjson.support.spring.annotation.FastJsonView) ResponseJSONP(com.alibaba.fastjson.support.spring.annotation.ResponseJSONP) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 5 with ResponseJSONP

use of com.alibaba.fastjson.support.spring.annotation.ResponseJSONP in project fastjson by alibaba.

the class FastJsonViewAndJSONPControllerTest method test1.

@ResponseJSONP
@RequestMapping("test1")
@FastJsonView(include = { @FastJsonFilter(clazz = Company.class, props = { "id", "name" }) })
public Company test1() {
    Company company = new Company();
    company.setId(100L);
    company.setName("测试");
    company.setDescription("fastjsonview注解测试");
    company.setStock("haha");
    return company;
}
Also used : Company(com.alibaba.json.test.entity.Company) FastJsonView(com.alibaba.fastjson.support.spring.annotation.FastJsonView) ResponseJSONP(com.alibaba.fastjson.support.spring.annotation.ResponseJSONP) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

ResponseJSONP (com.alibaba.fastjson.support.spring.annotation.ResponseJSONP)6 FastJsonView (com.alibaba.fastjson.support.spring.annotation.FastJsonView)4 Company (com.alibaba.json.test.entity.Company)4 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)4 JSONPObject (com.alibaba.fastjson.JSONPObject)2 Department (com.alibaba.json.test.entity.Department)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 ServletServerHttpRequest (org.springframework.http.server.ServletServerHttpRequest)2