developing a webservice using java and mysql

后端 未结 3 1274
暗喜
暗喜 2021-01-26 15:25

I am not familiar with websrvices and mysql ..i followed this http://www.vogella.com/articles/REST/article.html tutorial and developed a RESTful web service in Java with the JAX

相关标签:
3条回答
  • 2021-01-26 15:43

    The example you linked to does not actually make use of a database but uses in-memory Todo's to provide the data. In section 8 the author states

    Create the following data model and a Singleton which serves as the data provider for the model. We use the implementation based on an enumeration.

    The data model is the Todo class.

    The data provider is the TodoDao enum. The purpose of TodoDao, is essentially to store Todo's in memory. In other words, it performs the function that would otherwise be done by a database.

    What therefore needs to be done, is to:

    1. Replace TodoDao with a database.
    2. Map Todo to a table in the database.
      • To connect Java objects to a database, you can use an Object Relational Mapper (ORM), which can be achieved by using the Java Persistence API (JPA).
      • Therefore, to map Todo to a database table, it needs to be annotated with JPA annotations, thus creating a JPA Entity.

    Have a look the accepted answer to REST/JSON Web Services Java EE Framework, it should shed some light on what needs to be done. Part 1 covers creating a database, while Part 2 covers creating and annotating JPA entities (Part 3 - JAXB Bindings for xml or json, Part 4 - The RESTFul Service, Part 5 - The Client).

    If difficulties are still encountered, have a look at the answer I posted for Need to write a RESTful JSON service in Java, that should be suitable for someone who wants more nitty-gritty to, as a starting point, connect to a single table in a database and create a RESTful Web Service with JSON/XML representations using the following.

    • IDE: Eclipse IDE for Jave EE Developers (Kepler), comes with Maven built in
    • Database: MySQL (also makes use of MySQL Workbench)
    • Application Server: GlassFish 4.0
    • Java EE 7 (JAX-RS, JPA, JAXB, etc)
    • Any REST Client for testing: (eg Postman)
    0 讨论(0)
  • 2021-01-26 15:56

    I would start here : http://wiki.restlet.org/docs_2.1/13-restlet/21-restlet.html

    You can create a java class/method that encapsulates the Business logic, for example a method like getData(DataFormat xml/html, whatData) which connects to the mysql database and retrieves records and then transforms it into required format, call this method within getXML() and getHTML()

    0 讨论(0)
  • 2021-01-26 16:03

    Spring MVC makes REST based development very easy. Refer to this blog

    0 讨论(0)
提交回复
热议问题