Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7819485
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T06:58:15+00:00 2026-06-02T06:58:15+00:00

I’m building a simple RESTFul Service; and for achieve that I need two tasks:

  • 0

I’m building a simple RESTFul Service; and for achieve that I need two tasks:

  • Get an instance of my resource (i.e Book) from request parameters, so I can get that instance to be persisted
  • Build an XML document from that instance to send the representation to the clients

Right now, I’m doing both things in my POJO class:

public class Book implements Serializable {

    private Long id;

    public Book(Form form) {
        //Initializing attributes
        id = Long.parseLong(form.getFirstValue(Book.CODE_ELEMENT));
    }

    public Element toXml(Document document) {
        // Getting an XML Representation of the Book
        Element bookElement = document.createElement(BOOK_ELEMENT);
    }

I’ve remembered an OO principle that said that behavior should be where the data is, but now my POJO depends from Request and XML API’s and that doesn’t feels right (also, that class has persistence anotations)

Is there any standard approach/pattern to solve that issue?

EDIT:
The libraries i’m using are Restlets and Objectify.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T06:58:18+00:00Added an answer on June 2, 2026 at 6:58 am

    I agree with you when you say that the behavior should be where the data is. But at the same time, as you say I just don’t feel confortable polluting a POJO interface with specific methods used for serialization means (which can grow considerably depending on the way you want to do it – JSON, XML, etc.).

    1) Build an XML document from that instance to send the representation to the clients

    In order to decouple the object from serialization logic, I would adopt the Strategy Pattern:

    interface BookSerializerStrategy {
        String serialize(Book book);
    }
    
    public class XmlBookSerializerStrategy implements BookSerializerStrategy {
    
        public String serialize(Book book) {
            // Do something to serialize your book.
        }
    
    }
    
    public class JsonBookSerializerStrategy implements BookSerializerStrategy {
    
        public String serialize(Book book) {
            // Do something to serialize your book.
        }
    
    }
    

    You POJO interface would become:

    public class Book implements Serializable {
    
        private Long id;
        private BookSerializerStrategy serializer
    
        public String serialize() {
            return serializer.serialize(this);
        }
    
        public void setSerializer(BookSerializerStrategy serializer) {
            this.serializer = serializer;
        }
    }
    

    Using this approach you will be able to isolate the serialization logic in just one place and wouldn’t pollute your POJO with that. Additionally, returning a String I won’t need to couple you POJO with classes Document and Element.

    2) Get an instance of my resource (i.e Book) from request parameters, so I can get that instance to be persisted

    To find a pattern to handle the deserialization is more complex in my opinion. I really don’t see a better way than to create a Factory with static methods in order to remove this logic from your POJO.

    Another approach to answer your two questions would be something like JAXB uses: two different objects, an Unmarshaller in charge of deserialization and a Marshaller for serialization. Since Java 1.6, JAXB comes by default with JDK.

    Finally, those are just suggestions. I’ve become really interested in your question actually and curious about other possible solutions.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I am reading a book about Javascript and jQuery and using one of the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.