- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在尝试让我的 Spring rest Controller 返回 jsonp 但我并不高兴
如果我想返回 json,完全相同的代码可以正常工作,但我需要返回 jsonp我添加了一个转换器,我在网上找到了用于执行 jsonp 转换的源代码
我正在使用 Spring 版本 4.1.1.RELEASE 和 Java 7
非常感谢任何帮助
这是有问题的代码
mvc-dispatcher-servlet.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<bean id="contentNegotiationManager" class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
<property name="favorPathExtension" value="false" />
<property name="favorParameter" value="true" />
<property name="parameterName" value="mediaType" />
<property name="ignoreAcceptHeader" value="false"/>
<property name="useJaf" value="false"/>
<property name="defaultContentType" value="application/json" />
<property name="mediaTypes">
<map>
<entry key="atom" value="application/atom+xml" />
<entry key="html" value="text/html" />
<entry key="jsonp" value="application/javascript" />
<entry key="json" value="application/json" />
<entry key="xml" value="application/xml"/>
</map>
</property>
</bean>
<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<property name="contentNegotiationManager" ref="contentNegotiationManager" />
<property name="viewResolvers">
<list>
<bean class="org.springframework.web.servlet.view.BeanNameViewResolver" />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/templates/slim/${views.template.directory}/" />
<property name="suffix" value=".jsp" />
</bean>
</list>
</property>
<property name="defaultViews">
<list>
<bean class="com.webapp.handler.MappingJacksonJsonpView" />
</list>
</property>
</bean>
</beans>
com.webapp.handler.MappingJacksonJsonpView
package com.webapp.handler;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.view.json.MappingJackson2JsonView;
public class MappingJacksonJsonpView extends MappingJackson2JsonView {
/** Local log variable. **/
private static final Logger LOG = LoggerFactory.getLogger(MappingJacksonJsonpView.class);
/**
* Default content type. Overridable as bean property.
*/
public static final String DEFAULT_CONTENT_TYPE = "application/javascript";
@Override
public String getContentType() {
return DEFAULT_CONTENT_TYPE;
}
/**
* Prepares the view given the specified model, merging it with static
* attributes and a RequestContext attribute, if necessary.
* Delegates to renderMergedOutputModel for the actual rendering.
* @see #renderMergedOutputModel
*/
@Override
public void render(Map<String, ?> model, HttpServletRequest request, HttpServletResponse response) throws Exception {
LOG.info("Entered render Method :{}", request.getMethod());
if("GET".equals(request.getMethod().toUpperCase())) {
LOG.info("Request Method is a GET call");
Map<String, String[]> params = request.getParameterMap();
if(params.containsKey("callback")) {
String callbackParam = params.get("callback")[0];
LOG.info("callbackParam:{}", callbackParam);
response.getOutputStream().write(new String(callbackParam + "(").getBytes());
super.render(model, request, response);
response.getOutputStream().write(new String(");").getBytes());
response.setContentType(DEFAULT_CONTENT_TYPE);
}
else {
LOG.info("Callback Param not contained in request");
super.render(model, request, response);
}
}
else {
LOG.info("Request Method is NOT a GET call");
super.render(model, request, response);
}
}
}
有问题的 Controller 方法
@RequestMapping(value = { "/sources"}, method = RequestMethod.GET,
produces={MediaType.ALL_VALUE,
"text/javascript",
"application/javascript",
"application/ecmascript",
"application/x-ecmascript",
"application/x-javascript",
MediaType.APPLICATION_JSON_VALUE})
@ResponseBody
public Object getSources(@PathVariable(value = API_KEY) String apiKey,
@RequestParam(value = "searchTerm", required = true) String searchTerm,
@RequestParam(value = "callBack", required = false) String callBack) {
LOG.info("Entered getSources - searchTerm:{}, callBack:{} ", searchTerm, callBack);
List<SearchVO> searchVOList = myServices.findSources(searchTerm);
if (CollectionUtils.isEmpty(searchVOList)) {
LOG.error("No results exist for the searchterm of {}", searchTerm);
return searchVOList;
}
LOG.debug("{} result(s) exist for the searchterm of {}", searchVOList.size(), searchTerm);
LOG.info("Exiting getSources");
return searchVOList;
}
**Jquery Ajax代码**
$.ajax({
type: "GET",
url: "http://localhost:8080/my-web/rest/sources,
data: {
"searchTerm": request.term
},
//contentType: "application/json; charset=utf-8",
//dataType: "json",
contentType: "application/javascript",
dataType: "jsonp",
success: function(data) {
alert("success");
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
alert("Failure");
}
});
我得到的错误堆栈跟踪片段如下
org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation
at org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor.writeWithMessageConverters(AbstractMessageConverterMethodProcessor.java:168) ~[spring-webmvc-4.1.1.RELEASE.jar:4.1.1.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor.writeWithMessageConverters(AbstractMessageConverterMethodProcessor.java:101) ~[spring-webmvc-4.1.1.RELEASE.jar:4.1.1.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.handleReturnValue(RequestResponseBodyMethodProcessor.java:198) ~[spring-webmvc-4.1.1.RELEASE.jar:4.1.1.RELEASE]
at org.springframework.web.method.support.HandlerMethodReturnValueHandlerComposite.handleReturnValue(HandlerMethodReturnValueHandlerComposite.java:71) ~[spring-web-4.1.1.RELEASE.jar:4.1.1.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:122) ~[spring-webmvc-4.1.1.RELEASE.jar:4.1.1.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:781) ~[spring-webmvc-4.1.1.RELEASE.jar:4.1.1.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:721) ~[spring-webmvc-4.1.1.RELEASE.jar:4.1.1.RELEASE]
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:83) ~[spring-webmvc-4.1.1.RELEASE.jar:4.1.1.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:943) ~[spring-webmvc-4.1.1.RELEASE.jar:4.1.1.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:877) ~[spring-webmvc-4.1.1.RELEASE.jar:4.1.1.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:966) [spring-webmvc-4.1.1.RELEASE.jar:4.1.1.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:857) [spring-webmvc-4.1.1.RELEASE.jar:4.1.1.RELEASE]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:620) [servlet-api.jar:na]
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842) [spring-webmvc-4.1.1.RELEASE.jar:4.1.1.RELEASE]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727) [servlet-api.jar:na]
最佳答案
如 spring.io 上所述 blog关于 Spring 4.1 版本:
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 取自 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".
你不需要自己实现这些东西。只需重用 Spring 框架中的位即可。
以下简单的 Spring Boot 应用程序演示了在 Spring MVC 4.1 中使用内置 JSONP 支持。示例至少需要 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.<HttpMessageConverter<?> >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
将 POJO 转换为 JSONP
响应:
test({"attr":"demo"});
URL http://localhost:8080/demo2?callback=test
将 String
转换为 JSONP
响应:
test("demo2");
关于java - Spring Rest 和 Jsonp,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26974835/
如果可能的话,我想在答案中举一个具体的例子。 为了解释起见,我们这里有三个玩家。 我的服务器 (myserver.com) 客户端服务器 (myclient.com) 客户用户(通过 myclient
我想通过 JSONP 提供 Web 服务,并且想知道是否需要清理回调参数中的值。 我当前的服务器端脚本目前看起来像这样(或多或少。代码在 PHP 中,但实际上可以是任何东西。): header("Co
我有一个每分钟更新一次数据的 Web 服务器,并且希望使该数据可供所有类型的客户端使用。为了减少带宽,我使用 IF-MODIFIED-SINCE 和/或 IF-NONE-MATCH 设置 PHP 脚本
关闭。这个问题是opinion-based 。目前不接受答案。 想要改进这个问题吗?更新问题,以便 editing this post 可以用事实和引文来回答它。 . 已关闭 7 年前。 Improv
我正在努力使用 vue-resource 发出 JSONP 请求。任何人都可以提供一些工作示例来演示定义 jsonp 回调、处理 Vue 组件中的调用等的正确方法。 谢谢 **编辑:**对于其他人,让
我正在尝试获取 AngularJS 中 rest api 的 JSON 输出。以下是我面临的问题: Rest api url 中有端口号,AngularJS 为变量插入端口号。为此,我尝试了多种解决方
代码: 当我在浏览器中输入网址时,我可以看到所有对象,例如: link 但我似乎无法让它在我的代码中工作,我是否遗漏了什么? angular.module('ionicApp', ['ionic']
我在连接最近了解 JSONP 的一些点时遇到了麻烦。这是我的理解: 由于同源策略,禁止任何内容(包括 JSON)的跨域 XmlHttpRequests。这可以防止 XSRF。 您可以拥有一个带有返回
我正在使用 JSONP 进行 Ajax 调用以从第三方(与我的站点不同的域)获取 JSON 数据,它在 Chrome 和 Firefox 上工作但在 IE 上失败(9 和 10 是我试过的版本) .在
django rest 框架发行说明声称在 0.3.0 中重新添加了 JSONP 支持。我还在框架中找到了一个 JSONPRenderer 类。但是,我似乎找不到有关如何使用该东西的任何文档.....
我有一个使用 JSONP 的 WCF 服务(托管在 Azure 中)。它在 HTTP 上工作得很好,即,如果它仅接收 JSON,则仅返回 JSON,如果它接收 JSONP,则返回 JSONP。但是,一
有人知道为什么我的ajax失败吗? 我从这里通过 JSONP 获取数据: JSON Data 您可以使用此代码,并注意“警报”如何永远不会执行: $(document).ready(function(
我正在使用 jaubourg 的 jquery-jsonp module从与我的网站所在的域不同的域中获取数据。我从 jQuery 的 ajax 开始使用这个模块函数不支持 JSONP 请求的错误处理
我有一个使用 Jquery Mobile 制作移动 Web 应用程序的项目。我希望我的应用程序能够使用来自跨域 REST Web 服务的数据。很多人说用JSONP。但我仍然困惑如何使用它以及如何构建支
我正在使用 Angular 的 $http.jsonp() 请求,它成功返回了包含在函数中的 json: var url = "http://public-api.wordpress.com/rest
基于 Please explain JSONP ,我知道可以使用 JSONP 来绕过同源策略。 但是为了做到这一点,页面必须使用 标签。 我知道页面可以动态地发出新的脚本标签,例如: docum
在 jQuery 中使用 JSONP 时是否可能捕获错误?我已经尝试了 $.getJSON 和 $.ajax 方法,但都不会捕获我正在测试的 404 错误。这是我尝试过的方法(请记住,这些都可以成功运
我正在为一个带有嵌入推文的页面使用内容安全策略,我收到了 https://cdn.syndication.twimg.com 的报告。 ,这在我的政策中是不允许的。 这是一个很长的政策,但相关部分
使用 mootools 和 JsonP 我在 Firefox 错误控制台中收到“无效标签”错误 JsonP 似乎可以工作(我正确地获取了数据) {"jsondata":[{"title":"title
JSONP (JSON with Padding) 是 JSON 的一种 使用模式,可以让网页从别的网站那获取数据,即跨域读取数据 为什么我们从不同的域(网站)访问数据需要一个特殊的技术(JSONP
我是一名优秀的程序员,十分优秀!