java - Jetty server configuration -
i use jetty 9 , have problems configuration. simple rests works fine. problem begun when tried add new headers requests , error handler. way able handle headers adding code every response:
return response.ok(murals) .header("access-control-allow-origin", "*") .header("access-control-allow-methods", "get, post, delete, put") .build();
server configuration:
server server = new server(9998); servletcontexthandler servletcontexthandler = new servletcontexthandler(server, "/", servletcontexthandler.sessions); servletcontexthandler.addfilter(guicefilter.class, "/*", enumset.allof(dispatchertype.class)); servletcontexthandler.addservlet(defaultservlet.class, "/"); resourceconfig rc = new resourceconfig() .register(filterheaders.class) .register(exceptionnotfound.class) .register(corsfilter.class); //doesnt work new servletholder(new servletcontainer(rc)); handlerwrapper han = new handlerwrapper(); han.sethandler(new abstracthandler() { @override public void handle(string target, request baserequest, httpservletrequest request, httpservletresponse response) throws ioexception, servletexception { request.setattribute("welcome","hello"); //doesn't work } }); servletcontexthandler.addfilter(filters.class, "/*", enumset.of(dispatchertype.request)); //doesnt work resourcehandler resourcehandler = resourcehandler(); servletcontexthandler.sethandler(resourcehandler); servletcontexthandler.sethandler(han); server.start(); server.join();
crosfilter class
public class corsfilter implements containerresponsefilter { @override public containerresponse filter(containerrequest request, containerresponse response) { 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"); return response; } }
how register headers? doing wrong? not use web configuration.
you mixing jersey 1.x jersey 2.x, should not done. filter class based on jersey 1.x. resourceconfig
jersey 2.x. know because jersey 1.x resourceconfig
doesn't have register()
method. jersey 1.x, howwe register above filter
resourceconfig.getcontainerresponsefilters().add(new corsfilter());
and enough. jersey 2.x not have way of adding filters. need register
everything.
that being said, if using jersey 2.x, highly suggest getting rid of jersey 1.x dependencies. after doing so, first thing notice filter class no longer compile. here's how refactored 2.x filter should like:
import java.io.ioexception; import javax.ws.rs.container.containerrequestcontext; import javax.ws.rs.container.containerresponsecontext; import javax.ws.rs.container.containerresponsefilter; @provider public class corsfilter implements containerresponsefilter { @override public void filter(containerrequestcontext request, containerresponsecontext response) throws ioexception { response.getheaders().add("access-control-allow-origin", "*"); response.getheaders().add("access-control-allow-headers", "origin, content-type, accept, authorization"); response.getheaders().add("access-control-allow-credentials", "true"); response.getheaders().add("access-control-allow-methods", "get, post, put, delete, options, head"); } }
using above filter should work.
Comments
Post a Comment