[JAX-RS] Access-Control-Allow-Origin as a Filter

So if you’re getting the Access-Control-Allow-Origin error in chrome. Then you need to be sending those headers with the response from the server. One solution would be to put it in the Response in each method. A better (and more scalable) solution is to use a filter. Then don’t forget to disable it in production.

[JAX-RS] Use @Provider to leverage JAX-RS provider auto-discovery feature.

There are at least 2 ways to register a JAX-RS resource such as a set of web methods or the filters. 1. The first and the verbose way is to override the getClasses method in a subclass of Application. Like this:

The drawback with this approach is that you have to explicitly add all…

[JAX-RS] Some good auth resources

http://stackoverflow.com/questions/26777083/best-practice-for-rest-token-based-authentication-with-jax-rs-and-jersey http://www.developerscrappad.com/1814/java/java-ee/rest-jax-rs/java-ee-7-jax-rs-2-0-simple-rest-api-authentication-authorization-with-custom-http-header/ https://jersey.java.net/documentation/latest/security.html https://www.ibm.com/support/knowledgecenter/SSAW57_8.5.5/com.ibm.websphere.nd.doc/ae/twbs_jaxrs_impl_securejaxrs_webcont.html      

[WTF] Criteria API

This is the best representation of what I want to say about JPA criteria API: http://www.odi.ch/weblog/posting.php?posting=667  

  The equivalent JPQL query is: [code]FROM Pet[/code] The equivalent SQL query is: [code]SELECT * FROM Pet[/code] Clearly SQL is too simple and boring. You really need a bullshit API to produce a simple SQL query. Of…

Java EE 7 WebSocket example

1. Createa a Java EE Web application called MyWebSocket for example. 2. In index.html:

2. Create a class Called WebSocketEndpoint. Here the most important annotations are @ServerEndpoint, @OnOpen, @OnClose and @OnError, in these methods you put whatever you want to get executed when these

3. The bean which will produce our fake data….

JSF simple ajax example

Here is a simple AJAX example in JSF (run on Glassfish). Just to note: for sending the value to the bean you use the execute attribute of f:ajax, and for displaying the output from the beans’s getter, you use the render attribute. 1. pom.xml

2. The xhtml page:

Hello from Facelets


[JAX-WS] Simple Dispatch Client Example

Here is an example of a Dispatch type Java SE WebService client. 1. A sample Web Service (Running as a Dynamic Web Application in Glassfish):

  2. Here is the client (A simple Java SE application). Make sure to start the previous application first:

This will generate a SOAP request like:


web.xml examples for all versions of Servlet

1. Servlet 3.1

2. Servlet 3.0

3. Servlet 2.5

4. Servlet 2.4

5. Servlet 2.3

[JMS] Standalone JMS Client

If you were trying to make a JMS client to run as a standalone application 1. Create a java project. 2. Add as dependencies: java-ee api and C:installsglassfishglassfish-4.1glassfishlibgf-client.jar (adjust path) 3. Create main class: [code language=”java”] package jmsclient; import java.util.Hashtable; import java.util.Properties; import java.util.logging.Level; import java.util.logging.Logger; import javax.annotation.Resource; import javax.ejb.Stateless; import javax.jms.Connection; import javax.jms.ConnectionFactory; import…