1

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
Jaedabee
  • 45
  • 7

1 Answers1

1

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.

BJ Hargrave
  • 9,324
  • 1
  • 19
  • 27
  • Perhaps I misunderstood some of the examples... is the intent, then, that methods should return a `String` where we then invoke a `DTOs` method to return it instead of returning a DTO directly? – Jaedabee Sep 15 '17 at 12:13
  • DTO objects are useful in their form and if you need to serialize/deserialize them, you need to use some code which does that to your desired format. For example, JSON, XML, YAML, ... The DTOs class is an API which has such methods for serialization and deserialization of DTO objects. – BJ Hargrave Sep 16 '17 at 10:39