Cosio
Cosio

Reputation: 121

rest(JAX-RS) web service with JPA entities and JAXB

Hello I'm trying to develop a web service. This service has REST architecture (JAX-RS). I want to implement JPA hibernate between my web service and my database (MySQL) and also I want to implement JAXB (XML client<->server communication). The problem is that there are a lot of tutorials mixing other frameworks that I haven't any idea like Spring and others and doesn't clarify my ideas. To sum up what I want to implement is:

Client<------XML Document------->| JAXB - JAX-RS - JPA | <----> Mysql DB

I want to run my web service on Apache Tomcat and I am developing it with Eclipse.

Does anybody know any good tutorial to develop this? Am I in the correct way? Any comment is greatly appreciated. Thanks so much.

Upvotes: 2

Views: 1197

Answers (2)

Kirill Z.
Kirill Z.

Reputation: 1

To run a webservice Tomcat deployment may be not the simpliest way. If you need keep it simple you can build a usual J2SE application and run it as a single jar. Java 6 and above already has everything necessary for this including an integrated http server. Take a look at this code: Main class:

      

    package com.qnium.test.webservice;

    import javax.xml.ws.Endpoint;

    public class WSPublisher {

        public static void main(String[] args) {

            try {
                Endpoint.publish("http://localhost:8080/WS", new WSImpl());
                System.out.println("Service is ready");
            }
            catch (Exception ex) {
                System.out.println("Couldn't publish service: " + ex);
            }
        }
    }

Define the interface:

    

    package com.qnium.test.webservice;

    import javax.jws.WebMethod;
    import javax.jws.WebService;
    import javax.jws.soap.SOAPBinding;
    import javax.jws.soap.SOAPBinding.Style;

    @WebService
    @SOAPBinding(style=Style.RPC)
    public interface WS {

        @WebMethod
        public String getHello();
    }

Implement the interface. Here you can add what ever logic you whish including db access via Hibernate

    

    package com.qnium.test.webservice;

    import javax.jws.WebService;

    @WebService(endpointInterface = "com.qnium.test.webservice.WS")
    public class WSImpl implements WS {

        @Override
        public String getHello()
            return "Hello from WS";
        }
    }

The above method may be not suitable for large scale applications that will need load balancing, clustering etc, but you will always be able to move this to application server when required.

Upvotes: 0

Goldbones
Goldbones

Reputation: 1457

I do not use Eclipse. I use Netbeans. For your prupose https://netbeans.org/kb/docs/websvc/rest.html is the best approach because it demonstrate the steps that you want to achieve. You also mislead some concepts.

Acordding to Oracle Documentation (which I strongly advice) - https://docs.oracle.com/javaee/7/tutorial/

JAX-RS is a Java programming language API designed to make it easy to develop applications that use the REST architecture. By default for the application/xml media type JAX-RS will use JAXB to convert the objects to/from XML.

So the next point to understand is why you need to use a web service. Imagine that you have an app that insert musics and playlists. Now you want to add a letter for a specific music. You will have a lot of work because you have to find millions of letters to introduce in database, etc. So you can can have a Web service client which access a Web Service that provides letters for a ton of musics.So you will make a call(client) -- the information will pass by Json/XML (XML Document)---will get transform to a Java Object (using JAX-RS)--you will introduce the letter in your database (Mysql DB)

If in contrary you want to create a Web Service, use Soap UI Free - a free and open source cross-platform Functional Testing solution, to simulate a client for your application. It´s really simple to understand, and it will be a great update for your current work.

Upvotes: 1

Related Questions