Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to serialize ANY Object into a URI?
    primarykey
    data
    text
    <p>My basic question: is there anything built that already does this automatically (doesn't have to be part of a popular library/package)? The main things I'm working with are Spring (MVC) and Jackson2.</p> <p>I understand there are a few manual ways to do this:</p> <ol> <li>Create a method in each class that serializes its specific properties into <code>property=value&amp;</code> form (kind of stinks because it's a bunch of logic duplication, I feel).</li> <li>Create a function that accepts an object, and uses reflection to dynamically read all the properties (I guess the getters), and build the string by getting each. I'm assuming this is how Jackson works for serialization/deserialization in general, but I really don't know.</li> <li>Use some feature of Jackson to customly serialize the object. I've researched custom serializers, but it seems they are specific to a class (so I'd have to create one for each Class I'm trying to serialize), while I was hoping for a generic way. I'm just having trouble understanding how to apply one universally to objects. A few of the links: <ul> <li><a href="http://techtraits.com/Programming/2011/11/20/using-custom-serializers-with-jackson/" rel="nofollow noreferrer">http://techtraits.com/Programming/2011/11/20/using-custom-serializers-with-jackson/</a></li> <li><a href="http://wiki.fasterxml.com/JacksonHowToCustomSerializers" rel="nofollow noreferrer">http://wiki.fasterxml.com/JacksonHowToCustomSerializers</a></li> </ul></li> <li>Use <code>ObjectMapper.convertValue(object, HashMap.class);</code>, iterate over the <code>HashMap</code>'s key/value pairs, and build the string (which is <strong>what I'm using now</strong>, but I feel the conversions are excessive?).</li> <li>I'm guessing there's others I'm not thinking of.</li> </ol> <p>The main post I've looked into is <a href="https://stackoverflow.com/questions/1780896/java-getting-the-properties-of-a-class-to-construct-a-string-representation">Java: Getting the properties of a class to construct a string representation</a></p> <p>My point is that I have several classes that I want to be able to serialize without having to specify something specific for each. That's why I'm thinking a function using reflection (#2 above) is the only way to handle this (if I have to do it manually).</p> <p>If it helps, an example of what I mean is with, say, these two classes:</p> <pre><code>public class C1 { private String C1prop1; private String C1prop2; private String C1prop3; // Getters and setters for the 3 properties } public class C2 { private String C2prop1; private String C2prop2; private String C2prop3; // Getters and setters for the 3 properties } </code></pre> <p>(no, the properties names and conventions are not what my actual app is using, it's just an example)</p> <p>The results of serializing would be <code>C1prop1=value&amp;C1prop2=value&amp;C1prop3=value</code> and <code>C2prop1=value&amp;C2prop2=value&amp;C2prop3=value</code>, but there's only one place that defines how the serialization happens (already defined somewhere, or created manually by me).</p> <p>So my idea is that I will have to end up using a form of the following (taken from the post I linked above):</p> <pre><code>public String toString() { StringBuilder sb = new StringBuilder(); try { Class c = Class.forName(this.getClass().getName()); Method m[] = c.getDeclaredMethods(); Object oo; for (int i = 0; i &lt; m.length; i++) if (m[i].getName().startsWith("get")) { oo = m[i].invoke(this, null); sb.append(m[i].getName().substring(3) + ":" + String.valueOf(oo) + "\n"); } } catch (Throwable e) { System.err.println(e); } return sb.toString(); } </code></pre> <p>And modify it to accept an object, and change the format of the items appended to the <code>StringBuilder</code>. That works for me, I don't need help modifying this now.</p> <p>So again, my main question is if there's something that already handles this (potentially simple) serialization instead of me having to (quickly) modify the function above, even if I have to specify how to deal with each property and value and how to combine each?</p> <p>If it helps, the background of this is that I'm using a <code>RestTemplate</code> (Spring) to make a GET request to a different server, and I want to pass a specific object's properties/values in the URL. I understand I can use something like:</p> <pre><code>restTemplate.getForObject("URL?C1prop1={C1Prop1}&amp;...", String.class, C1Object); </code></pre> <p>I believe the properties will be automatically mapped. But like I said, I don't want to have to make a different URL template and method for each object type. I'm hoping to have something like the following:</p> <pre><code>public String getRequest(String url, Object obj) { String serializedUri = SERIALIZE_URI(obj); String response = restTemplate.getForObject("URL?" + serializedUri, String.class); return response; } </code></pre> <p>where <code>SERIALIZE_URI</code> is where I'd handle it. And I could call it like <code>getRequest("whatever", C1Object);</code> and <code>getRequest("whateverElse", C2Object);</code>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload