-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCountryRestService.java
More file actions
59 lines (50 loc) · 1.42 KB
/
CountryRestService.java
File metadata and controls
59 lines (50 loc) · 1.42 KB
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
53
54
55
56
57
58
59
package org.arpit.java2blog.jaxrs;
import java.util.ArrayList;
import java.util.List;
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;
import org.arpit.java2blog.bean.Country;
/*
* author: Arpit Mandliya
* */
@Path("/countries")
public class CountryRestService {
@GET
@Produces(MediaType.APPLICATION_JSON)
public List<Country> getCountries()
{
List<Country> listOfCountries = new ArrayList<Country>();
listOfCountries=createCountryList();
return listOfCountries;
}
@GET
@Path("{id: \\d+}")
@Produces(MediaType.APPLICATION_JSON)
public Country getCountryById(@PathParam("id") int id)
{
List<Country> listOfCountries = new ArrayList<Country>();
listOfCountries=createCountryList();
for (Country country: listOfCountries) {
if(country.getId()==id)
return country;
}
return null;
}
// Utiliy method to create country list.
public List<Country> createCountryList()
{
Country indiaCountry=new Country(1, "India");
Country chinaCountry=new Country(4, "China");
Country nepalCountry=new Country(3, "Nepal");
Country bhutanCountry=new Country(2, "Bhutan");
List<Country> listOfCountries = new ArrayList<Country>();
listOfCountries.add(indiaCountry);
listOfCountries.add(chinaCountry);
listOfCountries.add(nepalCountry);
listOfCountries.add(bhutanCountry);
return listOfCountries;
}
}