Java springrest与Jsonp
我试图让我的Spring rest控制器返回jsonp,但我没有任何乐趣 如果我想返回json,同样的代码也可以,但是我需要返回jsonp 我在一个转换器中添加了我在线找到的用于执行jsonp转换的源代码 我使用的是SpringVersion4.1.1.RELEASE和Java7 非常感谢您的帮助 这是有问题的代码 mvc dispatcher servlet.xmlJava springrest与Jsonp,java,jquery,spring,rest,jsonp,Java,Jquery,Spring,Rest,Jsonp,我试图让我的Spring rest控制器返回jsonp,但我没有任何乐趣 如果我想返回json,同样的代码也可以,但是我需要返回jsonp 我在一个转换器中添加了我在线找到的用于执行jsonp转换的源代码 我使用的是SpringVersion4.1.1.RELEASE和Java7 非常感谢您的帮助 这是有问题的代码 mvc dispatcher servlet.xml <beans xmlns="http://www.springframework.org/schema/beans
<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>
我得到的错误stacktrace的一个片段如下
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中关于spring 4.1版本的说明: Jackson现在支持JSONP。对于响应体方法,声明 @ControllerAdvice,如下所示。仅用于基于视图的渲染 在上配置JSONP查询参数名称 […]在4.1中,@ControllerAdvice也可以实现 ResponseByAdvice在这种情况下,将在 控制器方法返回,但在响应写入和 因此承诺。这有许多有用的应用程序 @JsonView JSONP已经作为构建在其上的两个示例 Javadoc取自: 设置JSONP请求参数名称。每次请求都有一个 参数,生成的JSON将被包装到名为 由JSONP请求参数值指定。 默认情况下配置的参数名是“jsonp”和“callback” 你不需要自己去实现这些东西。只需重用Spring框架中的部分 Spring引导示例 下面的简单Spring引导应用程序演示了SpringMVC4.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);
}
}
URLhttp://localhost:8080/demo2?callback=test
将字符串
转换为JSONP
响应:
test({"attr":"demo"});
test("demo2");
没有阅读所有代码,但在javascript中,您错过了ajax
success
方法中的结束括号和分号:alert(“success”
应该是alert(“success”)
@Ivaylyo-很抱歉,现在更新了-出于隐私原因,我不得不在将其放到网上之前更改一些代码-很遗憾,我从未使用Spring执行过JSONP,但就我所熟悉的JSONP而言,它需要一个回调函数来将结果传递给。这与success
ajax处理程序不同ery ajax请求支持一个jsonpCallback
参数,该参数应该是回调函数的名称。检查以供参考,我希望这有助于解决问题。再次阅读您的问题表明,您可能需要为JSONP(应用程序/javascript)配置一个转换器带有spring控制器配置的数据类型。HttpMediaTypeNotAcceptableException
通常表示spring没有为请求中提供的数据类型找到合适的转换器。@Ivaylo-我有一个转换后的书面-它名为MappingJacksonJsonpView,并附加在消息中。jquery ca如果它正常工作,llback不会有问题,但我没有收到服务器的响应。转换器正在服务器上访问,然后异常正在出现。谢谢你指出这一点,我不知道在Spring 4.1ksokol中有此更新-我尝试过此解决方案,但我仍然没有体验到任何乐趣。你知道有y个spring示例应用程序目前正在使用此jsonp功能?首先,尝试在不使用javascript客户端的情况下通过浏览器访问端点,例如http://localhost:8080/demo?callback=test
。那么您是否收到jsonp响应?在jQuery代码段中,替换contentType:“应用程序/javascript”
带有数据类型:“jsonp”
。尝试了这两种建议-仍然在stacktrace中得到完全相同的错误-org.springframework.web.HttpMediaTypeNotAcceptableException:找不到可接受的表示在使用Spring的解决方案之前是否删除了自定义的映射JacksonJSONPView
?是否可以将配置和控制器拆分为b是最小值还是再次测试?
@ControllerAdvice
private static class JsonpAdvice extends AbstractJsonpResponseBodyAdvice {
public JsonpAdvice() {
super("callback");
}
}
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);
}
}
test({"attr":"demo"});
test("demo2");