Posts

Showing posts with the label Spring

spring-data-rest in Action

Image
What is spring-data-rest? spring-data-rest , a recent addition to the spring-data project, is a framework that helps you expose your entities directly as RESTful webservice endpoints. Unlike rails, grails or roo it does not generate any code achieving this goal. spring data-rest supports JPA, MongoDB, JSR-303 validation, HAL and many more. It is really innovative and lets you setup your RESTful webservice within minutes. In this example i'll give you a short overview of what spring-data-rest is capable of. Initial Configuration  I'm gonna use the new Servlet 3 Java Web Configuration instead of an ancient web.xml. Nothing really special here. public class WebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer { @Override protected Class <?>[] getRootConfigClasses () { return new Class <?>[]{ AppConfiguration . class }; } @Override protected Class <?>[] getServletConfigClasses () { return ...

Good Bye Redeployment. spring-loaded, an Opensource Classreloader

Redeem yourself from hot deployments and OutOfMemoryException. spring-loaded is an opensource classreloader and a promising alternative to JRebel . It does not offer as many features as JRebel does, and it does not support any framework just yet. Nevertheless, its a great tool that can save the time you are waiting for that servletcontainer to restart again. What it can do: add/modify/delete methods/fields/constructors modify annotations on types/methods/fields/constructors add/remove/change values in enum types What it cant: support framework specific changes like Spring MVC @RequestMappings change log configuration on the fly It is not getting the attention that it deserves, so give it a try. Just download from github already, and add the following to your JVM parameters: java -javaagent:<pathTo>/springloaded-{VERSION}.jar -noverify

Spring MVC - @RequestBody and @ResponseBody demystified

In this post i want to dig into spring mvc a little, revealing what happens behind the scenes when a request is converted to your parameter object and vice versa. Before we start, i want to explain the purpose of these annotations. What are @RequestBody and @ResponseBody for? They are annotations of the spring mvc framework and can be used in a controller to implement smart object serialization and deserialization. They help you avoid boilerplate code by extracting the logic of messageconversion and making it an aspect. Other than that they help you support multiple formats for a single REST resource without duplication of code. If you annotate a method with @ResponseBody, spring will try to convert its return value and write it to the http response automatically. If you annotate a methods parameter with @RequestBody, spring will try to convert the content of the incoming request body to your parameter object on the fly. Here is an example @Controller @RequestMapping ( value = "/...

Avoid too many Parameters using the Builder Pattern

Today i want to show you another way to use the Builder Pattern . From what i ' ve seen there are tons of examples of the Builder Pattern online. But they do all have one thing in common: They are used to create an Instance of an Object and "replace" its constructor. This way you can create a complex Object in a very clean and readable fashion using a Fluent Interface . Other than that, there is also a slightly different way you can benefit of the Builder Pattern. Think of a Method with like 10 Parameters. Thats not quite nice, and should not ever happen. Robert C. Martin recommends a maximum amount of 3 Parameters in his book: Clean Code . You should rather use some kind of a Config Object holding those 10 parameters, to clean it up. But lets say, you have a BookService Class with a find(..) method, that allows to search by 10 different parameters, and every single one of them is optional. You could use the Builder Pattern with some kind of Query Object to solve the pro...

Using the Fast Code Eclipse Plugin

Image
I recently discovered an eclipse plugin that I've been needing since I started coding. On several occasions I started down the path of writing my own but quickly got lost in the world of eclipse and ran out of time. Out of the box, the fast code plugin generates many types of code/xml, but the part that I really like is its support for custom templates. How to Write Your Own Template Here are the steps that I used to create my own template which generates a spring RowMapper for a JdbcDaoSupport class. Start with exporting templates using menu Fast Code | Templates | Export Templates : Choosing templates-config.xml (or all) creates a new project in my workspace: Next, I altered the exported templates-config.xml file and added my own custom template: <template type="Create Spring Detail Mapper"> <description></description> <variation></variation> <class-pattern></class-pattern> <getter-sett...