简体   繁体   中英

How are HTTP requests handled in Java Web Services?

I am new to web services and am going through the book Java Webservices by Martin Kalin. I have gone through the initial basic concept of it and have a question:

Say producer sends the HTTP request (containing SOAP Message envelope) to a Java web service ( consumer ). Is the request internally handled by Servlet which extracts the SOAP message and convert it to corresponding Java domain object and then service implementation bean is called?

This question generic irrespective of any off the shelf framework like Metro and Axis. Just consider below code

 Endpoint.publish("webserviceURL", new CustomerServiceImpl())

Now if consumer sends the request to webserviceURL , will it be handled by Servlet always at the entry point or handled in some other way? (As this is how web request is handled in any web application)

A Servlet is a Java class that can receive a HTTP request and send a response back, right?

That being said, you could (if you really really wanted), code a full-blown web service using only Servlets (using its methods doGet() and doPost() ).

You'd, for instance, get the HTTP request, byte by byte, make it into a String , parse it into a XML file and then interpret it as a SOAP Envelope (let's call this " plumbing work "), and only then would begin processing the actual request (what you really wanted in the first place).

After done processing the actual request, you'd have to do some more plumbing work to convert the objects back into XML, and SOAP Envelopes (and don't forget about the hassle of handling eventual faults/exceptions).

You ask:

Is the request internally handled by servlet which extracts the soap message and convert it to corresponding java domain object (...)

The answer is no . The Servlets do nothing other than receiving the bytes and let you manipulate them in a Java method (eg doGet() or doPost() ). You could do that manually, but the conversion of the data into domain objects is usually done by a Web Service framework .

Now if consumer sends the request to webservice URL, will it be handled by servlet always at the entry point or handled in some other way?

The first one. It will always be handled by Servlets at the entry point. But you may ask:

Well, what is the point of a Web Service API like JAX-RS, Spring-WS or JAX-RS, then?

All Java's famous web services APIs are built on top of the Servlet API.

The Servlets do are the first to handle the request, but this "handling" made is very little: as I said, they only get the HTTP request bits and let you work with them as a Java method.

After that, the WS frameworks can do for you a bunch of other plumbing work . For instance, they can get the HTTP POST request and do all the Bytes<->XML<->SOAP Envelope<->Objects conversion to you in a transparent way. They'll also help you defining standard Fault envelopes, generating WSDL files automatically, and everything else.

Bottom line:

You can do all that yourself using only Servlets, but with a Web Service framework, you won't have to worry about all the plumbing work needed, and can go just about creating methods to handle the actual requests.

Edit:

Just to make it clear: Servlets aren't by any chance the only way to handle HTTP requests with Java.

They are, though, the somewhat standard way of doing it in Java EE, as they are one of the most stable and undisputed of the Java EE APIs , but there are other ways, of course - there always are.

What I said is the most famous frameworks are built on top of the Servlet API, just to point a few:

  • JAX-RS:
    • Metro
    • Axis
    • Axis2
    • CXF
    • JBossWS
  • JAX-RS:
    • Jersey,
    • RESTeasy;
    • CXF,
    • Restlet (the name says it all...)
  • Others:
    • Spring-WS,
    • Spring MVC.

And this is, as I said, just to point a few.

Requests are handled by Something That Can Handle HTTP Requests, period.

It's convenient to use servlets or filters because the plumbing has already been written for you in the form of the servlet/application container, but this isn't a requirement. For example, a service running on Netty wouldn't need to follow the servlet spec. (Although IIRC there's a layer to allow Netty to call servlets directly.)

As long as both the client and server are talking HTTP the underlying implementation doesn't matter, and doesn't need to be servlets–it just needs to talk HTTP.

All the web services work on top of servlets only be it SOAP or REST. Depending on which web service framework you use the servlet mapping will be configured such as jersey, resteasy etc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM