In my earlier submit I talk about about the right way to write a simple REST web service using jersey, at this time we talk about extra on it and reveal how one can create customized java class with JAXB and cope with XML response utilizing jersey.
Let’s first create eclipse Java EE venture and add all relying jersey jar recordsdata in classpath.
Edit your net.xml to seem like much like following.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
xml model=“1.0” encoding=“UTF-8”?>
<net–app xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”
xmlns=“http://java.solar.com/xml/ns/javaee” xmlns:net=“http://java.solar.com/xml/ns/javaee/web-app_2_5.xsd”
xsi:schemaLocation=“http://java.solar.com/xml/ns/javaee http://java.solar.com/xml/ns/javaee/web-app_3_0.xsd”
id=“WebApp_ID” model=“3.0”>
<show–identify>RESTfulTest</show–identify>
<servlet>
<servlet–identify>jersey–serlvet</servlet–identify>
<servlet–class>com.solar.jersey.spi.container.servlet.ServletContainer</servlet–class>
<init–param>
<param–identify>com.solar.jersey.config.property.packages</param–identify>
<param–worth>org.techzoo.restful</param–worth>
</init–param>
<load–on–startup>1</load–on–startup>
</servlet>
<servlet–mapping>
<servlet–identify>jersey–serlvet</servlet–identify>
<url–sample>/relaxation/*</url–sample>
</servlet–mapping>
</net–app>
|
We create Automotive
class and make it JAXB appropriate so it is going to be simple for jersey to return response in XML-format. Your class ought to like under. The annotation is self-explanatory.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
bundle org.techzoo.restful;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(identify=“car-details”)
public class Automotive {
personal int carId;
personal String carName, make;
personal double value;
public Automotive(){}
public Automotive(int carId, String carName,
String make, double value) {
tremendous();
this.carId = carId;
this.carName = carName;
this.make = make;
this.value = value;
}
@XmlAttribute(identify = “car-id”)
public int getCarId() {return carId;}
public void setCarId(int carId)
{this.carId = carId;}
@XmlElement (identify=“car-name”)
public String getCarName() {return carName;}
public void setCarName(String carName)
{this.carName = carName;}
@XmlElement
public String getMake() {return make;}
public void setMake(String make)
{this.make = make;}
@XmlElement
public double getPrice() {return value;}
public void setPrice(double value)
{this.value = value;}
@Override
public String toString() {
return String.format(“Automotive Particulars “ +
“[Name=%s, Make=%s, Price=%f]”,
carName, make, value);
}
}
|
Custom-made exception class for correct exception propagation to shopper.
1
2
3
4
5
6
|
bundle org.techzoo.restful;
public class CarNotFoundException extends RuntimeException {
public CarNotFoundException(){tremendous();}
public CarNotFoundException(String msg){tremendous(msg);}
}
|
Now write net service class which makes use of Automotive class and return response to shopper in XML format. We use @Produces (MediaType.APPLICATION_XML)
annotation for returning XML format.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
bundle org.techzoo.restful;
import java.util.HashMap;
import java.util.Map;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
@Path(“/xml/automobile”)
public class CarWebService {
personal Map<String, Automotive> automobiles;
public CarWebService(){
automobiles = new HashMap<String, Automotive>();
automobiles.put(“nano”, new Automotive(1,“Nano”,“TATA”,165000));
automobiles.put(“tavera”, new Automotive(2, “Tavera”, “Chervolet”, 865000));
automobiles.put(“scorpio”, new Automotive(2, “Scorpio”, “Mahindra”, 870000));
}
@GET
@Path(“/{identify}”)
@Produces (MediaType.APPLICATION_XML)
public Automotive getCarDetailsByName(@PathParam(“identify”) String identify) {
if(automobiles.containsKey(identify)) return automobiles.get(identify);
else throw new CarNotFoundException(
“Automotive with identify = “+identify+” not discovered.”);
}
@GET
@Path(“/{identify}/{make}”)
@Produces (MediaType.APPLICATION_XML)
public Automotive getCarDetailsByNameAndMake(@PathParam(“identify”) String identify,
@PathParam(“make”) String make)
{
if(automobiles.containsKey(identify)){
Automotive automobile = automobiles.get(identify);
if(automobile.getMake().equals(make)) {
return automobile;
}
else throw new CarNotFoundException(
“Automotive with identify = “+identify+” would not belong to “+make);
}
else throw new CarNotFoundException(
“Automotive with identify = “+identify+” not discovered.”);
}
}
|
After publishing this net service you possibly can entry this by means of browser utilizing following URL.
http://localhost:8080/RESTfulTest/relaxation/xml/automobile/nano
Write java shopper to entry this net service…
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
bundle org.techzoo.shoppers;
import org.techzoo.restful.Automotive;
import com.solar.jersey.api.shopper.Consumer;
import com.solar.jersey.api.shopper.ClientResponse;
import com.solar.jersey.api.shopper.WebResource;
/**
* @writer Tousif Khan
**/
public class CarWebServiceClient {
personal static ultimate String URL =
“http://localhost:8080/RESTfulTest/relaxation/xml/automobile/scorpio”;
public static void predominant(String[] args) throws Exception {
attempt {
Consumer shopper = Consumer.create();
WebResource webResource = shopper.useful resource(URL);
WebResource.Builder builder = webResource.settle for(“utility/xml”);
ClientResponse response = builder.get(ClientResponse.class);
if (response.getStatus() != 200) {
throw new RuntimeException(“Failed : HTTP error code : “+
response.getStatus());
}
Automotive automobile = response.getEntity(Automotive.class);
System.out.println(automobile);
} catch (Exception e) {
e.printStackTrace();
}
}
}
|
Supply techzoo.org