java web service

1.What is web service

    It is used to communicate from one system to another system over the internet even they are build in different programming language.

2.Types of web service

  • soap(simple object access protocol) is xml based and the java-ws(java api for xml web service) is its API.

  • restful(representational state transfer) is an architectural style not a protocol and jax-rs(java api for restful web service) is its API.

web service api

3.SOA

     SOA is abbreviation of service oriented architectural which is a design pattern. It is designed to provide services to other application through protocol. It is a concept only and  not tied to any programming language or platform.Web service is a technology of SOA.

4.WSDL

     WSDL is an acronym web services description language. It's a xml document containing information about web services such as method name, method parameter and how to access it. We can type “http://<ip>:<port>/ws/hello?wsdl ” in browser to see WSDL.

       elements of WSDL are :

  • Types element describes the data types used by your web service. Data types are usually specified by XML schema. It can be described in any language as long as your web service API supports it.

  • Binding element describes how your web service is bound to a protocol. In other  words, how your web service is accessible. To be accessible, the web service must be reachable using some network protocol. This is called 'binding' the web service to the protocol.

  • Interface element describes the operations supported by your web service. It is similar to methods in programming languages. Client can only call one operation per request.

  • Service element shows the endpoint of your web service. In other words, the address where the web services can be reached.

  • Endpoint illustrates the address of your web service. The endpoint binding attribute indicates what binding element this endpoint uses, i.e. protocol with which you will access web service. The address attribute describes the URI at which you can access the service.

  • Message element describes the data being exchanged between the web service provider and consumer.

5.UDDI

    It represents universal description, discovery and integration. It is a xml based framework for describing, discovering and integrating web services. UDDI is a directory of web service interfaces described by WSDL which containing information about web services.

6.How to build web service

code

SOAP

RPC

  • Defines an interface with annotation @WebService, @SOAPBinding(style=Style.RPC) at class level and @WebMethod at method level

  • Defines an implementation class with annotation @WebService(endpointInterface=”<interface name with package name>”) at class level

  • Publish this service with code : Endpoint.publish(address, implementator), the address looks like : http://192.168.2.102:7779/ws/hello

  • Client side has to define the interface with same package name as server side and the code are :

URL url = new URL(“<address>?wsdl”);

QName qName = new Qname(

http://<package name>”,”implementator name”);

Service service = Service.create(url,qName);

<Interface variable> =

service.getPort(<interface class>);

Last step is call the service method.

Document

It's similar to RPC, just replace the Style.RPC with Style.DOCUMENT

REST

Jersey

Jersey is an implementation of jax-rs.

  • In server side(dynamic web project), the code is :

@Path("/hello")

public class Hello {

@GET

@Produces(MediaType.TEXT_PLAIN)

public String sayPlainTextHello() {

return "Hello Jersey Plain";

}

  • the web.xml looks like :

<servlet>

<servlet-name>Jersey REST Service</servlet-name>

<servlet-class>

org.glassfish.jersey.servlet.ServletContainer

</servlet-class>

<init-param>

<param-name>

jersey.config.server.provider.packages

</param-name>

<param-value>cn.my.webservice.restful</param-value>

</init-param>

<load-on-startup>1</load-on-startup>

</servlet>

<servlet-mapping>

<servlet-name>Jersey REST Service</servlet-name>

<url-pattern>/rest/*</url-pattern>

</servlet-mapping>

  • In client side, the code is :

ClientConfig config = new ClientConfig();

Client client = ClientBuilder.newClient(config);

URI uri =

UriBuilder.fromUri(“http://<ip>:<port>/<app name>”).build();

WebTarget target = client.target(uri);

target.path(<value of url pattern except “/*”>)

.path(<value of @Path>).request()

.accept(MediaType.<?>).get(<return class>);

RESTeasy

RESTEasy is the jax-rs implementation provoded by JBoss.

jersey

GET

@GET

@Path("/{param}")

public Response getMsg(@PathParam("param") String msg) {

String output = "Jersey say "+msg;

return  Response.status(200).entity(output).build();

}

POST

@POST

@Path("/add")

public Response getUser(@FormParam("id") int id,

@FormParam("name") String name,

@FormParam("price") int price) {

return Response.status(200).entity("..).build();

}

File upload

@POST

@Path("/upload")

@Consumes(MediaType.MULTIPART_FORM_DATA)

public Response uploadFile(@FormDataParam("file") InputStream is,

@FormDataParam("file") FormDataContentDisposition fileDetail) {

String fileLocation = "E:/"+fileDetail.getFileName();

FileOutputStream os = new FileOutputStream(new File(fileLocation));

int read = 0;

byte[] bytes = new byte[1024];

while((read = is.read(bytes)) != -1) {

os.write(bytes, 0, read);

}

os.flush();

os.close();

String output = "file successly uploaded to : "+fileLocation;

return Response.status(200).entity(output).build();

File download

@GET

@Path("/txt")

@Produces("text/plain")

public Response getFile() {

String filePath="E:/test.txt";

File file = new File(filePath);

ResponseBuilder builder = Response.ok((Object)file);

builder.header("Content-Disposition", "attachment;filename=\"javapoint.txt\"");

return builder.build();

}

Written on March 26, 2016