Scala + Resteasy + Jetty 9 + Jackson

I have been start playing with scala and rest. Being a scala newbie I struggled a bit, but that's the best way to learn right ? Here a simple sbt project that expose a json rest api in Scala https://github.com/undeploy/scala-resteasy-jetty


Playing around with ios

Here a little project I worked this weekend that shows how to take a picture and the gps position on ios, hope can be helpful to some other that like me i starting playing around with Object-C and ios : https://github.com/undeploy/iMovi


AngularJS template and single quote

Are you using AngularJS template ? Well the single quote in the ng-include are important! If you don't type that the template will not work ;)
<div ng-include="'/header.html'"></div>



If you want to configure the ObjectMapper to serialize date as string well here it is.

First you create your custom implementation of ContextResolver<ObjectMapper> :

public class MyJacksonConfig  implements ContextResolver<ObjectMapper> {
  public ObjectMapper getContext(Class type) {
   ObjectMapper mapper = new ObjectMapper();
   mapper.configure(Feature.WRITE_DATES_AS_TIMESTAMPS, false);   
new Pair(
new JacksonAnnotationIntrospector(),
new JaxbAnnotationIntrospector()));
   return mapper;

then you just need to add the provider to the resteasy.providers list in the web.xml :


and voila', your DateTime will be serialized as ISO string format (eg: Created:2013-02 04T21:07:00.688+01:00)

Hope this will save some of your time guys :)


JAXB inheritance and XmlElementRef

If you want JAXB to use the subclass element root name (e.g in this case NotEmpty, NotNull), you have to indicate so adding the @XmlElementRef annotation on the collection containing the list of objects hierarchy. 

@XmlRootElement(name = "Property")
public class Property  {

public enum Type {

@XmlElement(name = "Name")
private String name;

@XmlElement(name = "Type")
private Type type;

@XmlElementWrapper(name = "Constraints")
private List<PropertyConstraint> constraints;

public String getName() {
return name;

public void setName(String name) {
this.name = name;

public Type getType() {
return type;

public void setType(Type type) {
this.type = type;

public void setConstraints(List<PropertyConstraint> constraints) {
this.constraints = constraints;

public List<PropertyConstraint> getConstraints() {
return constraints;

@XmlRootElement(name = "NotEmpty")
public class NotEmptyContraint extends PropertyConstraint {

@XmlRootElement(name = "NotNull")
public class NotNullConstraint extends PropertyConstraint {

public abstract class PropertyConstraint {

public static void main(String[] args) throws JAXBException {
Property p = new Property();
List<PropertyConstraint> asList = Arrays.asList(new PropertyConstraint[] {new NotEmptyContraint(), new NotNullConstraint()});

JAXBContext jaxc = JAXBContext.newInstance(Property.class, NotNullConstraint.class, NotEmptyContraint.class);
Marshaller m = jaxc.createMarshaller();
m.marshal(p, System.out);

will print : 

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>


Convert Java bean to Map using Groovy

Persisting java bean into MongoDB, means that you have to create a DBObject.
The easiest way to do it is to implement for each bean you want to persist a kind of mapper that will populate the DBObject adding all the bean's fields you want to persist.
But, what if you would to have something more general ? Well the answer seems to be relatively simple: transform the bean into a Map, then new BasicDBObject(map), done! Ok, have you tried to transform a java bean into a LinkedHashMap ?
If you google for it, you will find tons of java libraries that do all kind of bean to bean mapping, reflection and all sort of bean utils! But nothing really simple like BeanUtils.toMap(bean).

So here a very simple Groovy script that will achieve the task :

 def Map toMap(object) { return object?.properties.findAll{ (it.key != 'class') }.collectEntries {
           it.value == null || it.value instanceof Serializable ? [it.key, it.value] : [it.key, toMap(it.value)]

and back to bean :

def toObject(map, obj) {
  map.each {
       def field = obj.class.getDeclaredField(it.key)
       if (it.value != null) {
            if (field.getType().equals(it.value.class)){
               obj."$it.key" = it.value
            }else if (it.value instanceof Map){
               def objectFieldValue = obj."$it.key"
              def fieldValue = (objectFieldValue == null) ? field.getType().newInstance() : objectFieldValue
              obj."$it.key" = toObject(it.value,fieldValue) 
   return obj;


Spring + JUnit + RestTemplate + Jetty + Jersey

After a whole day spent struggling, finally I managed to make it work. Now I can easily test my rest services.

@ContextConfiguration(locations = { "classpath:applicationContext.xml" })
public class UserResourceTest {
 @Autowired RestTemplate restTemplate;

 static public void setUp() throws Exception {
  Server server = new Server(8080);
  ServletContextHandler resource = new ServletContextHandler(ServletContextHandler.SESSIONS);


  resource.getInitParams().put("contextConfigLocation", "classpath:applicationContext.xml");

  ServletHolder servletDef = new ServletHolder(SpringServlet.class);
  resource.addServlet(servletDef, "/*");

  resource.addEventListener(new ContextLoaderListener());
  resource.addEventListener(new RequestContextListener());


 public void test_rest_crud() throws Exception {
  Object users = restTemplate.getForObject("http://localhost:8080/users", Object.class);