找到提供程序类..但未添加http头作为响应

时间:2019-03-01 11:18:47

标签: java rest

我正在通过react js调用其余端点,但是却收到cors错误,因此添加了过滤器以在响应中添加额外的标头,但在访问该端点时未添加标头

web.xml

    <?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>RestAppTrial</display-name>
  <welcome-file-list>
    <welcome-file>Client.html</welcome-file>
  </welcome-file-list>
  <servlet>
    <servlet-name>jersey-serlvet</servlet-name>
    <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
       <init-param>
          <param-name>com.sun.jersey.config.property.packages</param-name> 
          <param-value>com.bitwise.controller</param-value>
       </init-param>
       <init-param>
          <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
          <param-value>true</param-value>
        </init-param>
       <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
   <servlet-name>jersey-serlvet</servlet-name>
   <url-pattern>/rest/*</url-pattern>
</servlet-mapping>


</web-app>

我的控制器类

@Path("/customers")
public class Restcall {


     @GET
        @Path("/{cusNo}")
        @Produces("application/json")
        public Customer produceCustomerDetailsinJSON(@PathParam("cusNo") int no) {

            Customer cust = new Customer();        
                cust .setCustNo(no);
                cust .setName("Java4s");
                cust .setCountry("India");
            return cust;
        }
    }

添加了针对CORS错误的过滤器 dgfjdkxsgdhsutgxdgbxhcghustgdgjbdbgdjgdbgudtgd

   @Provider
    public class CORSFilter implements ContainerResponseFilter {

        @Override
         public ContainerResponse filter(ContainerRequest request,
                    ContainerResponse response) {
                System.out.println(response.getHttpHeaders()+"  "+response.getContainerRequest());
                response.getHttpHeaders().add("Access-Control-Allow-Origin", "*");
                response.getHttpHeaders().add("Access-Control-Allow-Headers",
                        "origin, content-type, accept, authorization");
                response.getHttpHeaders().add("Access-Control-Allow-Credentials", "true");
                response.getHttpHeaders().add("Access-Control-Allow-Methods",
                        "GET, POST, PUT, DELETE, OPTIONS, HEAD");
                response.getHttpHeaders().add("Cross-Origin Read Blocking", "*");

                return response;
            }

日志:(信息:找到提供程序类。)

 INFO: Scanning for root resource and provider classes in the packages:
          com.bitwise.controller
        Mar 01, 2019 4:21:58 PM com.sun.jersey.api.core.ScanningResourceConfig logClasses
        INFO: Root resource classes found:
          class com.bitwise.controller.Restcall
          class com.bitwise.controller.RestServiceFileDownloadJava4s
        Mar 01, 2019 4:21:58 PM com.sun.jersey.api.core.ScanningResourceConfig logClasses
        INFO: Provider classes found:
          class com.bitwise.controller.CORSFilter
        Mar 01, 2019 4:21:58 PM com.sun.jersey.server.impl.application.WebApplicationImpl _initiate
        INFO: Initiating Jersey application, version 'Jersey: 1.8 06/24/2011 12:17 PM'
        Mar 01, 2019 4:22:02 PM org.apache.coyote.AbstractProtocol start
        INFO: Starting ProtocolHandler ["http-nio-8080"]
        Mar 01, 2019 4:22:02 PM org.apache.coyote.AbstractProtocol start
        INFO: Starting ProtocolHandler ["ajp-nio-8009"]
        Mar 01, 2019 4:22:02 PM org.apache.catalina.startup.Catalina start
        INFO: Server startup in 34224 ms

0 个答案:

没有答案