专注人工智能在金融领域的应用

Apache CXF RESTFUL WebService 修改响应头信息支持ajax跨域请求

applicationContext.xml文件

<bean id= “jsonProvider” class=“cn.wangbaocai.JsonProvider” />
<jaxrs:server id= “webServices” address =“/”>
           <jaxrs:serviceBeans>
               <ref bean= “userRest” />
           </jaxrs:serviceBeans>
 
           <jaxrs:providers>
               <ref bean= “jsonProvider” />
           </jaxrs:providers>
 
           <jaxrs:extensionMappings>
               <entry key= “json” value =“application/json” />
               <entry key= “xml” value =“application/xml” />
               <entry key= “zip” value =“application/zip” />
           </jaxrs:extensionMappings>
 
           <jaxrs:languageMappings>
               <entry key= “cn” value =“cn-ZH” />
           </jaxrs:languageMappings>
     </jaxrs:server >
 
 
JsonProvider类如下:
import java.io.IOException;
import java.io.OutputStream;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
 
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
 
import org.codehaus.jackson.jaxrs.JacksonJsonProvider;
 
public class JsonProvider extends JacksonJsonProvider {
 
     @Override
     public void writeTo(Object value, Class<?> type, Type genericType,
              Annotation[] annotations, MediaType mediaType,
              MultivaluedMap<String, Object> httpHeaders,
              OutputStream entityStream) throws IOException {
 
          List<Object> h = new ArrayList<Object>();
          h.add( “*”);
          httpHeaders.put( “Access-Control-Allow-Origin”, h);
 
           super.writeTo(value, type, genericType, annotations, mediaType,
                   httpHeaders, entityStream);
 
     }
 
}
点赞

发表评论

电子邮件地址不会被公开。 必填项已用*标注

您可以使用这些HTML标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>