Search code examples
javajsonosgienroute

Strings in OSGI Enroute DTOs are 'compressed'


My current task is to build an application using OSGI Enroute (http://enroute.osgi.org/) and Angular (though we elected to use Angular2/4 instead of the bundled AngularJS).

So far so good. I have a REST Java application which is responding to various requests from the Angular front-end but I'm currently running into an issue. In order to make development easier I am serving the Angular code on port 4200 and the back-end is listening on port 8080. CORS is working so I am able to send and receive requests while building the code. This may or may not be related to the issue.

The issue is when responding with a DTO with String content in excess of 21 characters the value is getting 'compressed.' I noticed this when attempting to use the value I received (a UUID) as a key for a subsequent GET request. Checking the DTO class I have confirmed that the toString() method does indeed call a private compress method where it will take any string longer than 21 characters and return something akin to this nine...last nine which tends to make it difficult to re-obtain a UUID from ... {"uuid":"95b90155-...ee5c02200", "name":"My Object"}...

So ... given something like this:

import org.osgi.dto.DTO;

public final class MyDTO extends DTO
{
   public String uuid;
   public String name;
}

and a REST application like this:

@RequireBootstrapWebResource(resource="css/bootstrap.css")
@RequireWebserverExtender
@RequireConfigurerExtender
@Component(name="web", propery={"debug=true"})
public final class MyApplication implements REST
{
   private boolean debug = false;

   public MyDTO getMe(RESTRequest request)
   {
      MyDTO dto = new MyDTO();
      dto.name = "My Object";
      dto.uuid = UUID.randomUUID().toString();
      return dto;
   }

   @SuppressWarnings("unused")
   @Activate
   void activate(ComponentContext component, BundleContext bundle, 
                 Map<String, Object> config)
   {
      if ("true".equals(config.get("debug"))
      {
         debug = true;
      }
   }
}

what am I missing in order to avoid this value 'compression' in my JSON responses?

Things I have tried

  • (The one that works) overriding the toString() method provided by DTO. This works but doesn't seem like it is the best solution. I would then have to override the toString() for anything that might have a string value in excess of 21 characters. The documentation indicates that the intent is for debugging, which likely means I'm not returning the proper type?
  • Setting the request's _response()'s content type to application/json: the result I see in the Chrome Web console is still a compressed string

Solution

  • I wrote the DTO.toString methods. It is clearly documented that the format of the output is not specified and that it is for use as a debugging tool and not for serialization. This is is why the impl "compresses" strings.

    If you need to serialize a DTO, you need to use code for that purpose. See https://github.com/osgi/osgi.enroute/blob/master/osgi.enroute.base.api/src/osgi/enroute/dto/api/DTOs.java for an API that can convert DTOs to a format like JSON.