CXF RESTFUL WebService 针对ajax 跨域问题


重写JSONProvider这个类

public class JsonProvider extends JSONProvider<Object> {

@Context
HttpServletRequest request;


@Override
public void writeTo(Object obj, Class<?> cls, Type genericType,
Annotation[] anns, MediaType m,
MultivaluedMap<String, Object> headers, OutputStream os)

throws IOException {
//获取jsonP传递过来的参数
String prefix = getContext().getHttpServletRequest().getParameter("callback");

boolean hasPrefix = !prefix.isEmpty();
if (hasPrefix) {
os.write(prefix.getBytes(HttpUtils.getSetEncoding(m, headers,"UTF-8")));
os.write('(');
}
super.writeTo(obj, cls, genericType, anns, m, headers, os);
if (hasPrefix) {
os.write(')');
}
}

}

配置cxf-restful.xml配置文件

<bean id="jsonProvider" class="com.tservice.sp.provider.JsonProvider"/>

<!-- 配置需要暴露的BeanService -->
<jaxrs:server id="restContainer" address="/spservice">
<jaxrs:serviceBeans>
  <ref bean="menuWebService"/>    
  <ref bean="cpWebService"/>
</jaxrs:serviceBeans>
<jaxrs:providers>
  <ref bean="jsonProvider"/>
</jaxrs:providers>
</jaxrs:server>

你可能感兴趣的:(CXF RESTFUL WebService 针对ajax 跨域问题)