Skip to main content

Posts

Showing posts from November, 2015

Type safety: Unchecked cast - Spring and Hibernate

Well, first of all, you're wasting memory with the new  HashMap  creation call. Your second line completely disregards the reference to this created hashmap, making it then available to the garbage collector. So, don't do that, use: private Map < String , String > someMap = ( HashMap < String , String >) getApplicationContext (). getBean ( "someMap" ); Secondly, the compiler is complaining that you cast the object to a  HashMap  without checking if it is a  HashMap . But, even if you were to do: if ( getApplicationContext (). getBean ( "someMap" ) instanceof HashMap ) { private Map < String , String > someMap = ( HashMap < String , String >) getApplicationContext (). getBean ( "someMap" ); } You would probably still get this warning. The problem is,  getBean  returns  Object , so it is unknown what the type is. Converting it to  HashMap  directly would not cause the problem with the sec...

Hypermedia as the engine of application HATEOAS

This information came to my knowledge when I was implementing a REST service for my company. A REST client enters a REST application through a simple fixed  URL .  All future actions the client may take are discovered within  resource  representations returned from the server. The  media types  used for these representations, and the  link relations  they may contain, are standardized. The client transitions through application states by selecting from the links within a representation or by manipulating the representation in other ways afforded by its media type. In this way, RESTful interaction is driven by hypermedia, rather than out-of-band information. reference -  https://en.wikipedia.org/wiki/HATEOAS To learn HATEOAS, https://spring.io/understanding/HATEOAS https://www.baeldung.com/spring-hateoas-tutorial

Filter requests for CORS

RESTful web service will include CORS access control headers in its response, you’ll need to write a filter that adds those headers to the response. This  SimpleCORSFilter class provides a simple implementation of such a filter: SimpleCORSFilter  responds to all requests with certain  Access-Control-* headers. In this case, the headers are set to allow POST, GET, OPTIONS, or DELETE requests from clients originated from any host. The results of a preflight request may be cached for up to 3,600 seconds (1 hour). And the request may include an  x-requested-with  header. https://spring.io/guides/gs/rest-service-cors/ http://www.codingpedia.org/ama/how-to-add-cors-support-on-the-server-side-in-java-with-jersey/