0

I have a POJO representing a record in DynmaoDB table:

class Request{
       String id,
       String name,
       Status statusName,
       }

where Status is

public enum Status{
  OPEN, REVIEW, APPROVED }

I want to get the String representing the fields in a fixed format, just like

Request_1 Name_1 OPEN

Request_2 empty_string APPROVED

empty_string represents an empty string which is used when the value of the field is not defined. What I am doing now is

String requestRecord = this.toString(request);

where the toString method is :

public String toString(Request request) {
        return new ReflectionToStringBuilder(request, new RecursiveToStringStyle()).toString();
      }

This gives me the output like this :

com.datamodel.Request@3bb5ceb[id=Request_1,name=,statusName=com.datatype.Status@2ffb3aec[statusName=Review,name=REVIEW,ordinal=1]]

Can anyone tell me how to get the string in the required format.?

Community
  • 1
  • 1
rightCoder
  • 281
  • 1
  • 3
  • 18
  • @Tunaki I want to use the relection (and that too recursively) so that I can write all the values seperated by space. I think thats not answered there. Can you provide some reference? – rightCoder May 31 '16 at 09:37
  • You're confusing `toString()` that you override from `Object` with your `toString(Request request)`. The output you have comes from the former. – Tunaki May 31 '16 at 09:39
  • 1
    @Tunaki doesn't look like an attempt to override `toString()`, rather an external string-serialization method somewhere else – zapl May 31 '16 at 09:43
  • @zapl The problem is `Request request` that is missing its `toString()`. `ReflectionToStringBuilder` is built by giving the object directly as argument, so it will use the default `toString()` to print the object. – Tunaki May 31 '16 at 09:45
  • @Tunaki String requestRecord = this.toString(request); System.out.println(requestRecord); – rightCoder May 31 '16 at 09:48
  • @Tunaki it doesn't, it actually uses class.getName + system.identityHashCode() as part of the string style. You can write your own [`ToStringStyle`](http://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/builder/ToStringStyle.html), e.g. like the json one. – zapl May 31 '16 at 09:57

0 Answers0