JAX-RS CORS无法正常工作

时间:2015-06-07 16:49:49

标签: jax-rs cors

ResponseBuilder defaultResponse = Response
        .status(200)
        .header("Access-Control-Allow-Origin", "*")
        .header("Access-Control-Allow-Headers", "origin, content-type, accept, authorization")
        .header("Access-Control-Allow-Credentials", "true")
        .header("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS, HEAD")
        .header("Access-Control-Max-Age", "1209600");

我有这个默认的响应构建器,并且:

@GET
@Path("/viviendas")
@Produces({ MediaType.APPLICATION_JSON })
public Response viviendas(@QueryParam("nelat") String nelat, @QueryParam("swlat") String wslat, @QueryParam("nelng") String nelng, @QueryParam("swlng") String swlng, @QueryParam("habs") String habs, @QueryParam("banios") String banios){

    List<String> datos = new ArrayList();
    datos.add(nelat);
    datos.add(wslat);
    datos.add(nelng);
    datos.add(swlng);

    List<Vivienda> v = vdao.consultaViviendas(datos);

    return defaultResponse
        .entity(v)
        .build();
}

但我仍然得到No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8084' is therefore not allowed access. The response had HTTP status code 500.

看起来标题设置不好还是什么?

我该如何解决?我尝试了很多东西,但仍处于相同的情况。

谢谢,我希望我的英语能被理解。

编辑2:我得到它的工作我回到POSTMAN中的json(休息测试chrome扩展名)直接返回列表

 @GET
@Path("/viviendas")
@Produces({ MediaType.APPLICATION_JSON })
public List<Vivienda> viviendas(@QueryParam("nelat") String nelat, @QueryParam("swlat") String wslat, @QueryParam("nelng") String nelng, @QueryParam("swlng") String swlng, @QueryParam("habs") String habs, @QueryParam("banios") String banios){

    List<String> datos = new ArrayList();
    datos.add(nelat);
    datos.add(wslat);
    datos.add(nelng);
    datos.add(swlng);

    List<Vivienda> v = vdao.consultaViviendas(datos);

    return v;
}

但在我的javascript客户端中,我仍然遇到错误No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8084' is therefore not allowed access. The response had HTTP status code 500.

javascript客户端:

 $.getJSON(url, function(result){


    if(result!=null){
    var inmuebles = result.inmuebles;
     if(inmuebles.length>1){
        $.each(inmuebles, function(i, inmueble){


         var ll = new google.maps.LatLng(inmueble.anuncioInmueble.latitud,inmueble.anuncioInmueble.longitud);
         var marker = new google.maps.Marker({
                            position: ll,
                            map: map,
                            icon: icons[selectedType],
                            id: inmueble.idInmueble

                          });
        google.maps.event.addListener(marker, 'click', function() {
                            var form;
                            switch(selectedType){
                                case 0: form = document.getElementById("form_vivienda");
                                    break;
                                case 1: form = document.getElementById("form_garaje");
                                    break;
                                case 2: form = document.getElementById("form_oficina");
                                    break;
                            }
                            form.find("id_d").value = inmueble.idInmueble;
                            form.submit();
        });





        $( "#result" ).append( "<form class='result_mini' action='detail' method='POST'><button id="+i+" class='btn btn-primary details' type=submit><p>"+inmueble.anuncioInmueble.direccion+"</p><p>"+inmueble.anuncioInmueble.precio+"</p></button><input id='id' type='hidden' name='id' value="+inmueble.idInmueble+"></form>" );

        addMarker(marker);

    });
     }

     else{

          var ll = new google.maps.LatLng(inmuebles.anuncioInmueble.latitud,inmuebles.anuncioInmueble.longitud);
         var marker = new google.maps.Marker({
                            position: ll,
                            map: map,
                            icon: icons[selectedType],
                            id: inmuebles.idInmueble

                          });
        google.maps.event.addListener(marker, 'click', function() {
                            var form;
                            switch(selectedType){
                                case 0: form = document.getElementById("form_vivienda");
                                    break;
                                case 1: form = document.getElementById("form_garaje");
                                    break;
                                case 2: form = document.getElementById("form_oficina");
                                    break;
                            }
                            form.find("id_d").value = inmueble.idInmueble;
                            form.submit();
        });





        $( "#result" ).append( "<form class='result_mini' action='detail' method='POST'><button id="+0+" class='btn btn-primary details' type=submit><p>"+inmuebles.anuncioInmueble.direccion+"</p><p>"+inmuebles.anuncioInmueble.precio+"</p></button><input id='id' type='hidden' name='id' value="+inmuebles.idInmueble+"></form>" );

        addMarker(marker);
     }




    $('.details').mouseenter(function() {
            toggleBounce(markers[this.id]);

          })
          .mouseleave(function() {
            toggleBounce(markers[this.id]);
          });

}
});



}, 500);

});

}

编辑: 这是我的服务器端日志,显然只有信息或警告

Grave:   235 [http-listener-1(2)] INFO org.hibernate.annotations.common.Version - Hibernate Commons Annotations 3.2.0.Final
Grave:   235 [http-listener-1(2)] INFO org.hibernate.cfg.Environment - Hibernate 3.6.10.Final
Grave:   251 [http-listener-1(2)] INFO org.hibernate.cfg.Environment - hibernate.properties not found
Grave:   251 [http-listener-1(2)] INFO org.hibernate.cfg.Environment - Bytecode provider name : javassist
Grave:   251 [http-listener-1(2)] INFO org.hibernate.cfg.Environment - using JDK 1.4 java.sql.Timestamp handling
Grave:   431 [http-listener-1(2)] INFO org.hibernate.cfg.Configuration - configuring from resource: /hibernate.cfg.xml
Grave:   431 [http-listener-1(2)] INFO org.hibernate.cfg.Configuration - Configuration resource: /hibernate.cfg.xml
Grave:   478 [http-listener-1(2)] WARN org.hibernate.util.DTDEntityResolver - recognized obsolete hibernate namespace http://hibernate.sourceforge.net/. Use namespace http://www.hibernate.org/dtd/ instead. Refer to Hibernate 3.6 Migration Guide!
Grave:   510 [http-listener-1(2)] INFO org.hibernate.cfg.Configuration - Reading mappings from resource : hibernate/entidades/AnuncioInmueble.hbm.xml
Grave:   526 [http-listener-1(2)] INFO org.hibernate.cfg.Configuration - Reading mappings from resource : hibernate/entidades/Favorito.hbm.xml
Grave:   543 [http-listener-1(2)] INFO org.hibernate.cfg.Configuration - Reading mappings from resource : hibernate/entidades/Provincias.hbm.xml
Grave:   559 [http-listener-1(2)] INFO org.hibernate.cfg.Configuration - Reading mappings from resource : hibernate/entidades/Garaje.hbm.xml
Grave:   575 [http-listener-1(2)] INFO org.hibernate.cfg.Configuration - Reading mappings from resource : hibernate/entidades/Vivienda.hbm.xml
Grave:   592 [http-listener-1(2)] INFO org.hibernate.cfg.Configuration - Reading mappings from resource : hibernate/entidades/Comunidades.hbm.xml
Grave:   593 [http-listener-1(2)] INFO org.hibernate.cfg.Configuration - Reading mappings from resource : hibernate/entidades/Municipios.hbm.xml
Grave:   624 [http-listener-1(2)] INFO org.hibernate.cfg.Configuration - Reading mappings from resource : hibernate/entidades/Usuario.hbm.xml
Grave:   626 [http-listener-1(2)] INFO org.hibernate.cfg.Configuration - Reading mappings from resource : hibernate/entidades/Oficina.hbm.xml
Grave:   643 [http-listener-1(2)] INFO org.hibernate.cfg.Configuration - Reading mappings from resource : hibernate/entidades/Foto.hbm.xml
Grave:   660 [http-listener-1(2)] INFO org.hibernate.cfg.Configuration - Reading mappings from resource : hibernate/entidades/Correo.hbm.xml
Grave:   660 [http-listener-1(2)] INFO org.hibernate.cfg.Configuration - Reading mappings from resource : hibernate/entidades/Opinion.hbm.xml
Grave:   676 [http-listener-1(2)] INFO org.hibernate.cfg.Configuration - Configured SessionFactory: null
Grave:   761 [http-listener-1(2)] INFO org.hibernate.cfg.HbmBinder - Mapping class: hibernate.entidades.AnuncioInmueble -> anuncio_inmueble
Grave:   819 [http-listener-1(2)] INFO org.hibernate.cfg.HbmBinder - Mapping class: hibernate.entidades.Favorito -> favorito
Grave:   819 [http-listener-1(2)] INFO org.hibernate.cfg.HbmBinder - Mapping class: hibernate.entidades.Provincias -> provincias
Grave:   819 [http-listener-1(2)] INFO org.hibernate.cfg.HbmBinder - Mapping class: hibernate.entidades.Garaje -> garaje
Grave:   819 [http-listener-1(2)] INFO org.hibernate.cfg.HbmBinder - Mapping class: hibernate.entidades.Vivienda -> vivienda
Grave:   819 [http-listener-1(2)] INFO org.hibernate.cfg.HbmBinder - Mapping class: hibernate.entidades.Comunidades -> comunidades
Grave:   819 [http-listener-1(2)] INFO org.hibernate.cfg.HbmBinder - Mapping class: hibernate.entidades.Municipios -> municipios
Grave:   819 [http-listener-1(2)] INFO org.hibernate.cfg.HbmBinder - Mapping class: hibernate.entidades.Usuario -> usuario
Grave:   819 [http-listener-1(2)] INFO org.hibernate.cfg.HbmBinder - Mapping class: hibernate.entidades.Oficina -> oficina
Grave:   819 [http-listener-1(2)] INFO org.hibernate.cfg.HbmBinder - Mapping class: hibernate.entidades.Foto -> foto
Grave:   819 [http-listener-1(2)] INFO org.hibernate.cfg.HbmBinder - Mapping class: hibernate.entidades.Correo -> correo
Grave:   819 [http-listener-1(2)] INFO org.hibernate.cfg.HbmBinder - Mapping class: hibernate.entidades.Opinion -> opinion
Grave:   835 [http-listener-1(2)] INFO org.hibernate.cfg.HbmBinder - Mapping collection: hibernate.entidades.AnuncioInmueble.opinions -> opinion
Grave:   835 [http-listener-1(2)] INFO org.hibernate.cfg.HbmBinder - Mapping collection: hibernate.entidades.AnuncioInmueble.favoritos -> favorito
Grave:   835 [http-listener-1(2)] INFO org.hibernate.cfg.HbmBinder - Mapping collection: hibernate.entidades.AnuncioInmueble.fotos -> foto
Grave:   835 [http-listener-1(2)] INFO org.hibernate.cfg.HbmBinder - Mapping collection: hibernate.entidades.Provincias.municipioses -> municipios
Grave:   835 [http-listener-1(2)] INFO org.hibernate.cfg.HbmBinder - Mapping collection: hibernate.entidades.Comunidades.provinciases -> provincias
Grave:   835 [http-listener-1(2)] INFO org.hibernate.cfg.HbmBinder - Mapping collection: hibernate.entidades.Municipios.anuncioInmuebles -> anuncio_inmueble
Grave:   835 [http-listener-1(2)] INFO org.hibernate.cfg.HbmBinder - Mapping collection: hibernate.entidades.Usuario.opinions -> opinion
Grave:   835 [http-listener-1(2)] INFO org.hibernate.cfg.HbmBinder - Mapping collection: hibernate.entidades.Usuario.correos -> correo
Grave:   835 [http-listener-1(2)] INFO org.hibernate.cfg.HbmBinder - Mapping collection: hibernate.entidades.Usuario.anuncioInmuebles -> anuncio_inmueble
Grave:   835 [http-listener-1(2)] INFO org.hibernate.cfg.HbmBinder - Mapping collection: hibernate.entidades.Usuario.favoritos -> favorito
Grave:   852 [http-listener-1(2)] INFO org.hibernate.cfg.Configuration - Hibernate Validator not found: ignoring
Grave:   891 [http-listener-1(2)] INFO org.hibernate.cfg.search.HibernateSearchEventListenerRegister - Unable to find org.hibernate.search.event.FullTextIndexEventListener on the classpath. Hibernate Search is not enabled.
Grave:   944 [http-listener-1(2)] INFO org.hibernate.connection.DriverManagerConnectionProvider - Using Hibernate built-in connection pool (not for production use!)
Grave:   944 [http-listener-1(2)] INFO org.hibernate.connection.DriverManagerConnectionProvider - Hibernate connection pool size: 20
Grave:   944 [http-listener-1(2)] INFO org.hibernate.connection.DriverManagerConnectionProvider - autocommit mode: false
Grave:   944 [http-listener-1(2)] INFO org.hibernate.connection.DriverManagerConnectionProvider - using driver: com.mysql.jdbc.Driver at URL: jdbc:mysql://localhost:3306/dametecho?zeroDateTimeBehavior=convertToNull
Grave:   944 [http-listener-1(2)] INFO org.hibernate.connection.DriverManagerConnectionProvider - connection properties: {user=root, password=****}
Grave:   1008 [http-listener-1(2)] INFO org.hibernate.dialect.Dialect - Using dialect: org.hibernate.dialect.MySQLDialect
Grave:   1031 [http-listener-1(2)] INFO org.hibernate.cfg.SettingsFactory - Database ->
       name : MySQL
    version : 5.5.27
      major : 5
      minor : 5
Grave:   1031 [http-listener-1(2)] INFO org.hibernate.cfg.SettingsFactory - Driver ->
       name : MySQL-AB JDBC Driver
    version : mysql-connector-java-5.1.23 ( Revision: ${bzr.revision-id} )
      major : 5
      minor : 1
Grave:   1034 [http-listener-1(2)] INFO org.hibernate.transaction.TransactionFactoryFactory - Using default transaction strategy (direct JDBC transactions)
Grave:   1040 [http-listener-1(2)] INFO org.hibernate.transaction.TransactionManagerLookupFactory - No TransactionManagerLookup configured (in JTA environment, use of read-write or transactional second-level cache is not recommended)
Grave:   1040 [http-listener-1(2)] INFO org.hibernate.cfg.SettingsFactory - Automatic flush during beforeCompletion(): disabled
Grave:   1040 [http-listener-1(2)] INFO org.hibernate.cfg.SettingsFactory - Automatic session close at end of transaction: disabled
Grave:   1040 [http-listener-1(2)] INFO org.hibernate.cfg.SettingsFactory - JDBC batch size: 15
Grave:   1040 [http-listener-1(2)] INFO org.hibernate.cfg.SettingsFactory - JDBC batch updates for versioned data: disabled
Grave:   1041 [http-listener-1(2)] INFO org.hibernate.cfg.SettingsFactory - Scrollable result sets: enabled
Grave:   1042 [http-listener-1(2)] INFO org.hibernate.cfg.SettingsFactory - JDBC3 getGeneratedKeys(): enabled
Grave:   1042 [http-listener-1(2)] INFO org.hibernate.cfg.SettingsFactory - Connection release mode: auto
Grave:   1043 [http-listener-1(2)] INFO org.hibernate.cfg.SettingsFactory - Maximum outer join fetch depth: 2
Grave:   1043 [http-listener-1(2)] INFO org.hibernate.cfg.SettingsFactory - Default batch fetch size: 1
Grave:   1043 [http-listener-1(2)] INFO org.hibernate.cfg.SettingsFactory - Generate SQL with comments: disabled
Grave:   1043 [http-listener-1(2)] INFO org.hibernate.cfg.SettingsFactory - Order SQL updates by primary key: disabled
Grave:   1043 [http-listener-1(2)] INFO org.hibernate.cfg.SettingsFactory - Order SQL inserts for batching: disabled
Grave:   1043 [http-listener-1(2)] INFO org.hibernate.cfg.SettingsFactory - Query translator: org.hibernate.hql.ast.ASTQueryTranslatorFactory
Grave:   1047 [http-listener-1(2)] INFO org.hibernate.hql.ast.ASTQueryTranslatorFactory - Using ASTQueryTranslatorFactory
Grave:   1047 [http-listener-1(2)] INFO org.hibernate.cfg.SettingsFactory - Query language substitutions: {}
Grave:   1047 [http-listener-1(2)] INFO org.hibernate.cfg.SettingsFactory - JPA-QL strict compliance: disabled
Grave:   1047 [http-listener-1(2)] INFO org.hibernate.cfg.SettingsFactory - Second-level cache: enabled
Grave:   1047 [http-listener-1(2)] INFO org.hibernate.cfg.SettingsFactory - Query cache: disabled
Grave:   1047 [http-listener-1(2)] INFO org.hibernate.cfg.SettingsFactory - Cache region factory : org.hibernate.cache.impl.NoCachingRegionFactory
Grave:   1047 [http-listener-1(2)] INFO org.hibernate.cfg.SettingsFactory - Optimize cache for minimal puts: disabled
Grave:   1047 [http-listener-1(2)] INFO org.hibernate.cfg.SettingsFactory - Structured second-level cache entries: disabled
Grave:   1062 [http-listener-1(2)] INFO org.hibernate.cfg.SettingsFactory - Statistics: disabled
Grave:   1062 [http-listener-1(2)] INFO org.hibernate.cfg.SettingsFactory - Deleted entity synthetic identifier rollback: disabled
Grave:   1062 [http-listener-1(2)] INFO org.hibernate.cfg.SettingsFactory - Default entity-mode: pojo
Grave:   1062 [http-listener-1(2)] INFO org.hibernate.cfg.SettingsFactory - Named query checking : enabled
Grave:   1062 [http-listener-1(2)] INFO org.hibernate.cfg.SettingsFactory - Check Nullability in Core (should be disabled when Bean Validation is on): disabled
Grave:   1081 [http-listener-1(2)] INFO org.hibernate.impl.SessionFactoryImpl - building session factory
Grave:   1097 [http-listener-1(2)] INFO org.hibernate.type.BasicTypeRegistry - Type registration [materialized_clob] overrides previous : org.hibernate.type.MaterializedClobType@409056b0
Grave:   1097 [http-listener-1(2)] INFO org.hibernate.type.BasicTypeRegistry - Type registration [wrapper_materialized_blob] overrides previous : org.hibernate.type.WrappedMaterializedBlobType@442b5ded
Grave:   1097 [http-listener-1(2)] INFO org.hibernate.type.BasicTypeRegistry - Type registration [clob] overrides previous : org.hibernate.type.ClobType@7b239591
Grave:   1097 [http-listener-1(2)] INFO org.hibernate.type.BasicTypeRegistry - Type registration [java.sql.Clob] overrides previous : org.hibernate.type.ClobType@7b239591
Grave:   1097 [http-listener-1(2)] INFO org.hibernate.type.BasicTypeRegistry - Type registration [characters_clob] overrides previous : org.hibernate.type.PrimitiveCharacterArrayClobType@5d30a1bf
Grave:   1097 [http-listener-1(2)] INFO org.hibernate.type.BasicTypeRegistry - Type registration [materialized_blob] overrides previous : org.hibernate.type.MaterializedBlobType@23cb3265
Grave:   1097 [http-listener-1(2)] INFO org.hibernate.type.BasicTypeRegistry - Type registration [wrapper_characters_clob] overrides previous : org.hibernate.type.CharacterArrayClobType@7159973e
Grave:   1097 [http-listener-1(2)] INFO org.hibernate.type.BasicTypeRegistry - Type registration [blob] overrides previous : org.hibernate.type.BlobType@30d8a195
Grave:   1097 [http-listener-1(2)] INFO org.hibernate.type.BasicTypeRegistry - Type registration [java.sql.Blob] overrides previous : org.hibernate.type.BlobType@30d8a195
Grave:   1487 [http-listener-1(2)] INFO org.hibernate.impl.SessionFactoryObjectFactory - Not binding factory to JNDI, no JNDI name configured
Información:   select distinct v from Vivienda v join v.anuncioInmueble i WHERE i.latitud=36.467690032731795 and i.longitud>=-8.68468585000005
Información:   0

0 个答案:

没有答案