0

I want to build a REST-API with Jax-Rs with cors enabled. So I googled how to do and found this:

http://www.developerscrappad.com/1781/java/java-ee/rest-jax-rs/java-ee-7-jax-rs-2-0-cors-on-rest-how-to-make-rest-apis-accessible-from-a-different-domain/

The solution is a ResponseFilter, that adds some header information to every response, so that the browser of the user knows, that cross domain accesses are allowed.

Because the ResponseFilter is not executed when I do any request (tried GET, POST and OPTIONS), I googled again and found this:

ContainerResponseFilter not working

@lefloh gave a good answer, that sounds logic (to remove the annotation @PreMatching). It did so, but nevertheless my filter is not invoked, when I do a Http-Request.

This is my code:

import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerResponseContext;
import javax.ws.rs.container.ContainerResponseFilter;
import javax.ws.rs.ext.Provider;
import java.io.IOException;

@Provider
public class RESTServiceResponseFilter implements ContainerResponseFilter {

    @Override
    public void filter(ContainerRequestContext parContainerRequestContext, ContainerResponseContext parContainerResponseContext) throws IOException {
        parContainerResponseContext.getHeaders().add( "Access-Control-Allow-Origin", "*" );
        parContainerResponseContext.getHeaders().add( "Access-Control-Allow-Credentials", "true" );
        parContainerResponseContext.getHeaders().add( "Access-Control-Allow-Methods", "GET, POST, DELETE, PUT" );
        parContainerResponseContext.getHeaders().add( "Access-Control-Allow-Headers", "Content-Type" );
    }
}

I kept on googling and found out, that I forgot to add the Filter to the web.xml. So I also did that:

<servlet>
    <display-name>webinterface.api</display-name>
    <servlet-name>JAX-RS REST Servlet</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>de.tsystems.lbus.apprestserver</param-value>
    </init-param>
    <init-param>
        <param-name>com.sun.jersey.spi.container.ContainerResponseFilters</param-name>
        <param-value>de.apprestserver.filter.RESTServiceResponseFilter</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>JAX-RS REST Servlet</servlet-name>
    <url-pattern>/TNR/*</url-pattern>
</servlet-mapping>

I have no more ideas now and want to ask you, if you maybe have a solution for me. Thanks in advance!

sideshowbarker
  • 81,827
  • 26
  • 193
  • 197
Maxi
  • 415
  • 4
  • 24

1 Answers1

1

You're using Jersey 1 (can tell by the com.sun.jersey in your web.xml). The filter implementation you are using (or showing us) is Jersey 2. There's subtle difference, but it's a major difference. The latter will not work with Jersey 1.

And the fact that it even compiles (if that's the case) means that you need to get rid of some dependencies. The class you have is a JAX-RS 2 class (interface). Any JAX-RS/Jersey 2 dependency, you might have, get rid of them. They don't play well (maybe not cause of issue, but get rid of them to drop any confusion)

  • Jersey 1 == com.sun.jersey (keep)
  • Jersey 2 == org.glassfish.jersey (get rid of)
  • JAX-RS 2 api == javax.ws.rs-api (get rid of)

See here for Jersey 1 implementation and configuration

Community
  • 1
  • 1
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
  • I tried now to write my ResponseFilter for Jersey 1 like in the link you gave me. It still does not execute. Now I want to do it in the reverse way, to remove all Jersey 1 dependencies. But I got 1 problem: When I remove all old depencies with com.sum.jersey what has the Servlet class ( com.sun.jersey.spi.container.servlet.ServletContainer) now to be? I found nothing only with JAX-RS2 api on the internet, only both mixed.. I have the feeling that I'm on the wrong way but I want to understand the failure. And yes everything compiles and works except the filter.. – Maxi Mar 17 '15 at 14:07
  • Are you using Maven? Or at least know how to use it? – Paul Samsotha Mar 17 '15 at 14:11
  • 1
    If so, create a project from an archetype, using [mvn command](https://jersey.java.net/documentation/latest/getting-started.html#new-webapp), or just use the Maven coordinates to start a project from your IDE. Either way you will get anew Jersey webapp, that uses JAX-RS 2. If you are just starting, you might as well start with JAX-RS 2, as it has more features. With this project, you can use the class you have provided in your code, with no extra configuration – Paul Samsotha Mar 17 '15 at 14:15
  • Also see [here for deployment options](https://jersey.java.net/documentation/latest/deployment.html#deployment.servlet) for Jersey 2 – Paul Samsotha Mar 17 '15 at 14:19
  • @ peesk illet's edit: sorry I don't get it anymore, why get rid of javax.ws.rs-api? I use the annotations like @ GET, @ PATH from that! Can you give a an example or tutorial how I have to do that? I tried it now several times, I never got an exception, always only 404 Error or nothing. Is therefore Jax-RS a good and clean choice to build a rest-api or should I maybe choose something different? – Maxi Mar 17 '15 at 15:47
  • That was for using Jersey 1. My answer is based on your original post. Jersey 1 uses jsr311 jar which is jax-rs 1. Now you are saying you want to use Jersey 2, which is jax-rs 2 and uses the `javax.ws.rs-api`. I just told yo what you need to do to get an app running. Just use that Maven archetype. It only has one Maven dependency, and pulls everything else in that you need. – Paul Samsotha Mar 17 '15 at 15:55
  • If you don't know how to use archetypes, then just create a new maven project, add [this dependency version 2.16](http://stackoverflow.com/a/27894395/2587435), then follow the instructions [here](https://jersey.java.net/documentation/latest/deployment.html#deployment.servlet) for how to configure the deployment. If you don't know how to use maven, then please do learn.. for your own sake. I don't know how else I can explain. I cannot give you a complete tutorial. I think the resource links I've provided should be enough help, given you know Maven. – Paul Samsotha Mar 17 '15 at 15:59
  • You can also download the [examples bundle](https://jersey.java.net/download.html) for complete examples. They all use Maven – Paul Samsotha Mar 17 '15 at 16:01
  • Hello peeskillet, apperently I don't know maven enough, to get the examples running. There I have to improve. Nevertheless this helped me to get an working and clean JAX-RS 2.0 application: http://stackoverflow.com/a/26721737/2264877 - Thank you very much for your help!! – Maxi Mar 18 '15 at 12:30