This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.mycompany.blooddonorswebservice; | |
import java.util.*; | |
import javax.ws.rs.*; | |
import javax.ws.rs.core.*; | |
@Path("/Donors") | |
public class DonorsService { | |
DataService d=new DataService(); | |
@GET | |
public List<Data> getDonors(@QueryParam("country") String country, | |
@QueryParam("bloodgroup") String bloodgroup){ | |
if(country!=null && bloodgroup!=null) | |
return d.getDonorsByCountryAndBloodGroup(country,bloodgroup); | |
else if(country!=null) | |
return d.getDonorsByCountry(country); | |
else if(bloodgroup!=null) | |
return d.getDonorsByBloodGroup(bloodgroup); | |
else | |
return d.getAllDonors(); | |
} | |
@POST | |
@Consumes(MediaType.APPLICATION_JSON) | |
public Response addDonors(Data data){ | |
return d.addDonor(data); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.mycompany.blooddonorswebservice; | |
public class Data { | |
int id; | |
String isim; | |
String soyisim; | |
String eposta; | |
String telefon; | |
String adres; | |
String kangrubu; | |
String sifre; | |
public int getId() { | |
return id; | |
} | |
public void setId(int id) { | |
this.id = id; | |
} | |
public String getIsim() { | |
return isim; | |
} | |
public void setIsim(String isim) { | |
this.isim = isim; | |
} | |
public String getSoyisim() { | |
return soyisim; | |
} | |
public void setSoyisim(String soyisim) { | |
this.soyisim = soyisim; | |
} | |
public String getEposta() { | |
return eposta; | |
} | |
public void setEposta(String eposta) { | |
this.eposta = eposta; | |
} | |
public String getTelefon() { | |
return telefon; | |
} | |
public void setTelefon(String telefon) { | |
this.telefon = telefon; | |
} | |
public String getAdres() { | |
return adres; | |
} | |
public void setAdres(String adres) { | |
this.adres = adres; | |
} | |
public String getKangrubu() { | |
return kangrubu; | |
} | |
public void setKangrubu(String kangrubu) { | |
this.kangrubu = kangrubu; | |
} | |
public String getSifre() { | |
return sifre; | |
} | |
public void setSifre(String sifre) { | |
this.sifre = sifre; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.mycompany.blooddonorswebservice; | |
import java.sql.*; | |
import java.util.ArrayList; | |
import java.util.List; | |
import javax.ws.rs.core.Response; | |
public class DataService { | |
Connection conn; | |
public void connect(){ | |
try{ | |
Class.forName("com.mysql.jdbc.Driver"); | |
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/kanbagisi","root",""); | |
}catch(Exception e){ | |
System.out.println("hata"+e.getMessage()); | |
} | |
} | |
public List<Data> getAllDonors(){ | |
connect(); | |
List<Data> donors=new ArrayList<>(); | |
try{ | |
PreparedStatement ps=conn.prepareStatement("Select isim,soyisim,eposta,adres from bilgiler"); | |
ResultSet rs=ps.executeQuery(); | |
while(rs.next()){ | |
Data b=new Data(); | |
b.setIsim(rs.getString("isim")); | |
b.setSoyisim(rs.getString("soyisim")); | |
b.setEposta(rs.getString("eposta")); | |
b.setAdres(rs.getString("adres")); | |
donors.add(b); | |
} | |
}catch(Exception e){ | |
System.out.println("hata"+e.getMessage()); | |
} | |
return donors; | |
} | |
public List<Data> getDonorsByCountry(String country){ | |
connect(); | |
int i=1; | |
List<Data> donors2=new ArrayList<>(); | |
try{ | |
PreparedStatement ps=conn.prepareStatement("Select isim,soyisim,eposta,adres from bilgiler where adres like %?%"); | |
ps.setString(1,country); | |
ResultSet rs=ps.executeQuery(); | |
while(rs.next()){ | |
Data co=new Data(); | |
co.setIsim(rs.getString("isim")); | |
co.setSoyisim(rs.getString("soyisim")); | |
co.setEposta(rs.getString("eposta")); | |
co.setAdres(rs.getString("adres")); | |
donors2.add(co); | |
} | |
}catch(Exception e){ | |
System.out.println("hata"+e.getMessage()); | |
} | |
return donors2; | |
} | |
//------------ | |
public List<Data> getDonorsByBloodGroup(String bloodGroup){ | |
connect(); | |
List<Data> donors3=new ArrayList<>(); | |
try{ | |
PreparedStatement ps=conn.prepareStatement("Select isim,soyisim,eposta,adres from bilgiler where kangrubu like %?%"); | |
ps.setString(1,bloodGroup); | |
ResultSet rs=ps.executeQuery(); | |
while(rs.next()){ | |
Data blo=new Data(); | |
blo.setIsim(rs.getString("isim")); | |
blo.setSoyisim(rs.getString("soyisim")); | |
blo.setEposta(rs.getString("eposta")); | |
blo.setAdres(rs.getString("adres")); | |
donors3.add(blo); | |
} | |
}catch(Exception e){ | |
// | |
} | |
return donors3; | |
} | |
public List<Data> getDonorsByCountryAndBloodGroup(String country,String bloodgroup){ | |
connect(); | |
List<Data> donors4=new ArrayList<>(); | |
try{ | |
PreparedStatement ps=conn.prepareStatement("Select isim,soyisim,eposta,adres from bilgiler where kangrubu like %?% and adres like %?%"); | |
ps.setString(1,bloodgroup); | |
ps.setString(2,country); | |
ResultSet rs=ps.executeQuery(); | |
while(rs.next()){ | |
Data tw=new Data(); | |
tw.setIsim(rs.getString("isim")); | |
tw.setSoyisim(rs.getString("soyisim")); | |
tw.setEposta(rs.getString("eposta")); | |
tw.setAdres(rs.getString("adres")); | |
donors4.add(tw); | |
} | |
}catch(Exception e){ | |
// System.out.println("hata"+e.getMessage()); | |
} | |
return donors4; | |
} | |
public Response addDonor(Data data){ | |
connect(); | |
try{ | |
PreparedStatement ps=conn.prepareStatement("insert into bilgiler(isim,soyisim,eposta,telefon" | |
+ ",adres,kangrubu,sifre) values(?,?,?,?,?,?,?)"); | |
ps.setString(1,data.getIsim()); | |
ps.setString(2,data.getSoyisim()); | |
ps.setString(3,data.getEposta()); | |
ps.setString(4,data.getTelefon()); | |
ps.setString(5,data.getAdres()); | |
ps.setString(6,data.getKangrubu()); | |
ps.setString(7,data.getSifre()); | |
ps.executeUpdate(); | |
return Response.ok("Başarılı").build(); | |
}catch(Exception e){ | |
System.out.println("hata"+e.getMessage()); | |
} | |
return Response.status(Response.Status.NOT_IMPLEMENTED).build(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.mycompany.blooddonorswebservice; | |
import java.io.IOException; | |
import org.codehaus.jackson.map.ObjectMapper; | |
public class jsonUtil { | |
private static ObjectMapper mapper; | |
static{ | |
mapper=new ObjectMapper(); | |
} | |
public static String convertJavaToJSon(Object object) throws IOException{ | |
String jsonResult=""; | |
try{ | |
jsonResult=mapper.writeValueAsString(object); | |
} | |
catch(Exception e){ | |
// System.out.println(e.getMessage()); | |
} | |
return jsonResult; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.ws.rs.core.*; | |
import javax.ws.rs.ApplicationPath; | |
@ApplicationPath("bloodcenter") | |
public class ApplicationConfig extends Application{ | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.mycompany.blooddonorswebservice; | |
import java.io.IOException; | |
import javax.ws.rs.client.*; | |
import javax.ws.rs.core.MediaType; | |
public class ClientApi { | |
public static void main(String []args) throws IOException{ | |
Client client=ClientBuilder.newClient(); | |
WebTarget t=client.target("http://localhost:8080/bloodDonorsWebService/bloodcenter/Donors"); | |
String response=t.request(). | |
post(Entity.entity(createDonor(),MediaType.APPLICATION_JSON)).toString(); | |
System.out.println(response); | |
} | |
public static String createDonor() throws IOException{ | |
Data d=new Data(); | |
d.setIsim("Gokhan"); | |
d.setKangrubu("AB+"); | |
d.setSoyisim("Guzelkokar"); | |
d.setAdres("Iskenderun/HATAY"); | |
d.setTelefon("496496496496"); | |
d.setEposta("ggggg@gmail.com"); | |
d.setSifre("Galatasaray"); | |
String donorJson=jsonUtil.convertJavaToJSon(d); | |
return donorJson; | |
} | |
} |
Kodlarımız gördüğünüz gibi.Öncelikle bu kodlar arasında bloğumuzda bulunmayan Jackson var.Jackson kullanarak Java object <-> JSON dönüşümü yaptık.Daha sonra dataservice kısmında sql sorgularımızı çalıştırdık.Projemiz basit olmasına rağmen önemli birkaç eksiği bulunuyor.Bunlardan bir taneside response mesajları.Çünkü herhangi bir sql exception'ini bile ne tür bir response mesajı olacağına karar vermemişiz.Bir dipnot düşmek istiyorum.Siz siz olun exception'larınızı kullanıcıya göstermeyin. :) Bu projeyi arasıra güncelleyeceğim.SAĞLICAKLA KALIN.
Hiç yorum yok:
Yorum Gönder