Yet Another Petstore !


Do you remember the good old Java Petstore ? It was a sample application created by Sun for its Java BluePrints program. The Java Petstore was designed to illustrate how J2EE (and then Java EE) could be used to develop an eCommerce web application. Yes, the point of the Petstore is to sell pets online.

The Petstore had a huge momentum and we started to see plenty of Petstore-like applications flourish (the idea was to build an entire application with a certain technology). But let’s face it, the J2EE version was far too complex and was using plenty of (today outdated) design patterns. When I wrote my Java EE 5 book back in 2006 (sorry, it’s written in French), I decided to write a Petstore-like application in Java EE 5 but much simpler. But again, it’s out-dated today.

Later on I wrote a book about Java EE 6and felt there was a need to have an updated Petstore so we could discover new patterns and APIs. Unfortunately Sun/Oracle didn’t do it… so I’ve decided to do it myself (with some help from my friends Antoine Sabot-DurandBrice Leporini, Hervé Le Morvan). I’ve updated my YAPS Petstore to follow Java EE 6 and all its goodies (CDI, EJB Lite, REST interface, Bean Validation).

The goals of this sample is to :

  • use Java EE 6 and just Java EE 6 : no external framework or dependency (I even use java.util.logging API ;o)
  • make it simple : no complex business algorithm, but I use most of the Java EE 6 APIs, the point is to bring Java EE 6 technologies together to create an eCommerce website

If you want to use a different web interface (PrimeFaces, pure HTML, Android…), external frameworks, add some sexy alternative JVM language… feel free to fork the code and do so. But I won’t do it because I want this EE 6 Petstore to remain simple and to stick to Java EE 6.


The only external frameworks that I’ve used are Twitter Bootstrap (so my JSF pages don’t look too ugly) and Arquillian for integration testing. My goal is to be able to deploy this Yaps Petstore on different application servers. For now it’s mostly GlassFish, JBoss but some work is also happening on TomEE and I’ll try to port it later on Resin, Weblogic, Websphere…. and who knows, do some performance benchmark of the same application but on different servers.

My next task is to deploy this Yaps Petstore on CloudBees… stay tuned. In the meantime, give it a try and let me know what you think.

22 thoughts on “Yet Another Petstore !

  1. Antonio una pregunta, veo que utilizas JSF y REST, pero existe realmente una interacción entre ellos o REST es únicamente para dejar expuestos los métodos para un posible uso.

    Disculpa por escribir en español.

    Saludos,

      1. Hi Antonio, Thanks for the app. Where can I find this component diagram? thanks..

  2. OMG! this is downloading 100s of jars n poms for last 28 mins…even when I have glassfish and arquillion in local repo..will this ever stop…what the hell I need those spring jars for…this example is supposed to demo java ee….Sir I have to work hard to buy bandwidth….I can’t understand when everyone trying to learn java ee6 is having glassfish(ee reference implementation) installed on local systems,why the most of examples come with pom n bom instead of build.xml…..aargh….huh..

  3. hola Antonio, soy nuevo con maven cuando corro mvn clean install -Pglassfish-embedded muestra el siguiente error:

    [INFO] Scanning for projects…
    [WARNING]
    Profile with id: ‘glassfish-embedded’ has not been activated.

    [INFO] ————————————————————————
    [INFO] Building Petstore application using Java EE 6
    [INFO] task-segment: [clean, install]
    [INFO] ————————————————————————
    [INFO] [clean:clean {execution: default-clean}]
    [INFO] Deleting file set: /home/martin/maven-pruebas/target (included: [**], excluded: [])
    [INFO] [resources:resources {execution: default-resources}]
    [INFO] Using ‘UTF-8’ encoding to copy filtered resources.
    [INFO] Copying 7 resources
    [INFO] [jrebel:generate {execution: generate-rebel-xml}]
    [INFO] Processing org.agoncal.application:petstoreee6 with packaging war
    [INFO] ————————————————————————
    [ERROR] BUILD ERROR
    [INFO] ————————————————————————
    [INFO] Error building POM (may not be this project’s POM).

    Project ID: org.glassfish.main:glassfish-parent:pom:3.1.2-b20

    Reason: Cannot find parent: net.java:jvnet-parent for project: org.glassfish.main:glassfish-parent:pom:3.1.2-b20 for project org.glassfish.main:glassfish-parent:pom:3.1.2-b20

    [INFO] ————————————————————————
    [INFO] For more information, run Maven with the -e switch
    [INFO] ————————————————————————
    [INFO] Total time: 4 seconds
    [INFO] Finished at: Fri Jul 13 23:13:16 ART 2012
    [INFO] Final Memory: 28M/165M
    [INFO] ————————————————————————

    Can you help me, thank you very much!!!

  4. Merci pour cette application modèle et aussi pour le très bon livre Beginning Java™ EE 6 Platform with GlassFish™ 3.

    Je travaille avec java depuis 2 ans mais ai encore des difficultés avec CDI, interfaces et tout simplement la réalisation complète d’application web.
    Cette application vient à point dans mon étude quotidienne de java.
    Pourtant un tutorial, ou un livre, pour créer ce Petstore ne serait pas superflu. Qu’en pensez-vous ?

    Il y a quelque temps déjà j’ai suivi le très bon tutorial de Andreas Manessinger
    http://programming.manessinger.com/tutorials/an-eclipse-glassfish-java-ee-6-tutorial/
    mais ce tutorial etait sans maven.

    Pour tester le Petstore vous avez utilisé Mockito.
    Sur ohloh j’ai comparé mockito et jmockit https://www.ohloh.net/p/compare?project_0=Mockito&project_1=JMockit
    J’ai l’impression que mockito n’est plus développé. Est-ce que vous avez une préférence particulière pour mockito ?

  5. Hi

    It would be possible to add security roles and make them work along with subject principals in a vendor independent way in the current state of the application ?

    I am a little confused about the pluggability of the LoginModules and how make them work without the vendor specific implementations

    Can anyone give me some guidelines?

    Sorry for my poor English.

    1. Adding other security roles in my todo list. I really need to write a bit more about security : it’s a very important topic but poorly documented in EE. Stay tune

      1. Hi Antonio,
        I did authentication with SSL certs in Glassfish, so I extended AppservCertificateLoginModule instead of LoginModule as you did.
        As it’s not possible to inject EJB in this class, I did it a vintage way :
        ic = new InitialContext();
        userEJB = (UserEJB) ic.lookup(“java:global/specify-it/UserEJB”);

        I noticed you did the trick another way (maybe more CDI oriented?) with BeanManager . Could you explain the difference in the two solutions (my simple lookup and your BeanManager stuff)?

  6. Bonjour Antonio,

    Très beau travail. Etant novice sur javaEE j’ai trouvé cet exemple passionnant (en plus du bouquin).

    J’aurai juste une question sur une annotation (@produces) que je ne comprends pas trop.
    dans la class : AccountController
    @Produces
    @LoggedIn
    private Customer loggedinCustomer;
    Qui appriori dit : Attention prêt pour l’injection (ou un truc du style)

    dans la class : ShoppingCartController
    @Inject
    @LoggedIn
    private Instance loggedInCustomer;
    Qui dit : inject l’instance de Customer.

    Faut-il obligatoirement un qualifier pour ce genre d’opération ? ou si ce n’est pas le cas, comment il se débrouille pour retrouver ses petits ?

    Encore merci pour ce sympathique exemple.
    PS : Tu penses essayer de le déployer sur openShift ?

  7. Hi Antonio,

    Very nice post as I am looking for tutorials about JavaEE.

    I bought your book overseas and I am about halfway thru and I certainly learned a lot from it as a javaee beginner. 🙂
    The book does not use any maven so its easy to follow along which is good for non-maven user like me.. 🙂

    Just a thought though, can you somehow create a tutorial for this discussing some of the important modules?
    I know that my request is hard and that you are busy and the task may take majority of your time.

    As a ‘newbie’ I find it hard to decipher everything here so I thought that ‘what if’ there is a documentation for this sample tutorial? I think that it would be too great since everything covered here is javaee-centered.
    I think everyone would link in at this site and make this their ‘official’ javaee tutorial site..

    But its just my thought though..I will try hard to understand everything.

    Thanks.

    Regards,
    Nel

  8. Just came across the PetStore application and was wondering if there is any option to run it using a database like mySQL.

  9. I recently downloaded src form Github

    using the mvn clean install -PJbossas7 managed i also get following error

    INFO] Scanning for projects…
    [WARNING]
    Profile with id: ‘glassfish-embedded’ has not been activated.

    [INFO] ————————————————————————
    [INFO] Building Petstore application using Java EE 6
    [INFO] task-segment: [clean, install]
    [INFO] ————————————————————————
    [INFO] [clean:clean {execution: default-clean}]
    [INFO] Deleting file set: /home/martin/maven-pruebas/target (included: [**], excluded: [])
    [INFO] [resources:resources {execution: default-resources}]
    [INFO] Using ‘UTF-8′ encoding to copy filtered resources.
    [INFO] Copying 7 resources
    [INFO] [jrebel:generate {execution: generate-rebel-xml}]
    [INFO] Processing org.agoncal.application:petstoreee6 with packaging war
    [INFO] ————————————————————————
    [ERROR] BUILD ERROR
    [INFO] ————————————————————————
    [INFO] Error building POM (may not be this project’s POM).

    Has this been resolved? it was reported above but reply is in French?

  10. Still works well. Today I cloned the github repo, ran mvn package, deployed to glassfish 4, and started the app. Thank you.

  11. Hi Antonio, it’s good to hear that updated java petstore is created, could you please release a book or provide step by step instructions pdf for the setup of this application?

    1. I’ll update the README and make sure everything works fine with the latest JBoss and GlassFish release.

  12. Hi Antonio,

    I have a problem with the .war. when I load the project in eclipse and generated Run as… -> Maven build…. and in goals put “package” and I click in apply and finally I click run.

    Then, the file applicationPetstore.war was generate…. ok… until here all well… but, when I am going to deploy the applicationPetstore.war in JBoss, this show an error as the following:

    Sat Nov 08 17:38:27 GMT-500 2014
    Failed to enable applicationPetstore.war.

    Unexpected HTTP response: 500

    Request
    {
    “address” => [(“deployment” => “applicationPetstore.war”)],
    “operation” => “deploy”
    }

    Response

    Internal Server Error
    {
    “outcome” => “failed”,
    “failure-description” => {
    “JBAS014671: Servicios fallidos” => {“jboss.deployment.unit.\”applicationPetstore.war\”.INSTALL” => “org.jboss.msc.service.StartException in service jboss.deployment.unit.\”applicationPetstore.war\”.INSTALL: JBAS018733: Error al procesar la fase INSTALL de deployment \”applicationPetstore.war\”
    Caused by: org.jboss.as.server.deployment.DeploymentUnitProcessingException: java.lang.ClassNotFoundException: org.apache.derby.jdbc.EmbeddedDataSource from [Module \”deployment.applicationPetstore.war:main\” from Service Module Loader]
    Caused by: java.lang.ClassNotFoundException: org.apache.derby.jdbc.EmbeddedDataSource from [Module \”deployment.applicationPetstore.war:main\” from Service Module Loader]”},
    “JBAS014771: Servicios con dependencias que faltan/no disponibles” => [“jboss.deployment.unit.\”applicationPetstore.war\”.weld.weldClassIntrospector falta [jboss.deployment.unit.\”applicationPetstore.war\”.beanmanager]”]
    },
    “rolled-back” => true
    }

    I do not have idea…. please help me.

Leave a Reply