Şimdi de Java'da yazdığımız kodları adım adım yorumlayalım.
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.yamlentityprovider; | |
import java.util.ArrayList; | |
import java.util.HashSet; | |
import java.util.List; | |
import java.util.Set; | |
import javax.ws.rs.Consumes; | |
import javax.ws.rs.DELETE; | |
import javax.ws.rs.GET; | |
import javax.ws.rs.PUT; | |
import javax.ws.rs.Path; | |
import javax.ws.rs.PathParam; | |
import javax.ws.rs.Produces; | |
import javax.ws.rs.core.MediaType; | |
@Path("employees") | |
public class EmployeeResource { | |
private static Set<Employee> employees=new HashSet<>(); | |
@PUT | |
@Consumes("application/yaml") | |
@Path("/{newId}") | |
public String create (Employee employee,@PathParam("newId") long newId){ | |
employee.setId(Long.valueOf(newId)); | |
if(employees.contains(employee)){ | |
throw new RuntimeException("Employee with id already exists :"+newId); | |
} | |
employees.add(employee); | |
return "Msg: employee created for id : "+newId; | |
} | |
@GET | |
@Produces(MediaType.TEXT_PLAIN) | |
public List<Employee> list(){ | |
return new ArrayList<>(employees); | |
} | |
@DELETE | |
public void deleteAll(){ | |
employees.clear(); | |
} | |
} |
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.yamlentityprovider; | |
import java.util.Objects; | |
public class Employee { | |
private long id; | |
private String name; | |
private String dept; | |
@Override | |
public boolean equals(Object o){ | |
if(this==o)return true; | |
if(o==null || getClass()!=o.getClass())return false; | |
Employee employee=(Employee) o; | |
return id==employee.id; | |
} | |
@Override | |
public int hashCode(){ | |
return Objects.hash(id); | |
} | |
public long getId() { | |
return id; | |
} | |
public void setId(long id) { | |
this.id = id; | |
} | |
public String getName() { | |
return name; | |
} | |
public void setName(String name) { | |
this.name = name; | |
} | |
public String getDept() { | |
return dept; | |
} | |
public void setDept(String dept) { | |
this.dept = dept; | |
} | |
} |
EmployeeResource ve Employee sınıflarımızda kayda değer birşey yok.Daha önce öğrendiğimiz ek açıklamalar ile basit bir servis yazılmış.Dikkat edilecek tek yer, @Consume("application/yaml") olarak belirtilmiş.
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.yamlentityprovider; | |
import javax.ws.rs.Consumes; | |
import javax.ws.rs.Produces; | |
import javax.ws.rs.WebApplicationException; | |
import javax.ws.rs.core.MediaType; | |
import javax.ws.rs.core.MultivaluedMap; | |
import javax.ws.rs.ext.MessageBodyReader; | |
import javax.ws.rs.ext.MessageBodyWriter; | |
import javax.ws.rs.ext.Provider; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.io.OutputStream; | |
import java.io.OutputStreamWriter; | |
import java.lang.annotation.Annotation; | |
import java.lang.reflect.Type; | |
import java.util.Scanner; | |
import org.yaml.snakeyaml.Yaml; | |
@Provider | |
@Consumes({"application/yaml", MediaType.TEXT_PLAIN}) | |
@Produces({"application/yaml", MediaType.TEXT_PLAIN}) | |
public class YamlEntityProvider<T> implements MessageBodyWriter<T>, MessageBodyReader<T> { | |
@Override | |
public boolean isReadable(Class<?> type, Type genericType, Annotation[] annotations, | |
MediaType mediaType) { | |
return true; | |
} | |
@Override | |
public T readFrom(Class<T> type, Type genericType, Annotation[] annotations, MediaType mediaType, | |
MultivaluedMap<String, String> httpHeaders, InputStream entityStream) | |
throws IOException, WebApplicationException { | |
Yaml yaml = new Yaml(); | |
T t = yaml.loadAs(toString(entityStream), type); | |
return t; | |
} | |
public static String toString(InputStream inputStream) { | |
return new Scanner(inputStream, "UTF-8") | |
.useDelimiter("\\A").next(); | |
} | |
@Override | |
public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, | |
MediaType mediaType) { | |
return true; | |
} | |
@Override | |
public long getSize(T t, Class<?> type, Type genericType, Annotation[] annotations, | |
MediaType mediaType) { | |
return -1; | |
} | |
@Override | |
public void writeTo(T t, Class<?> type, Type genericType, Annotation[] annotations, | |
MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, | |
OutputStream entityStream) throws IOException, WebApplicationException { | |
Yaml yaml = new Yaml(); | |
OutputStreamWriter writer = new OutputStreamWriter(entityStream); | |
yaml.dump(t, writer); | |
writer.close(); | |
} | |
} |
Gördüğünüz gibi YamlEntityProvider<T> adında bir entity class'ımız mevcut.Entity classlarının kullanım amaçlarından biride o sınıfta kullanılacak veri tipinin sabit olmasıdır.Buna bağlı olarak güvelik seviyesi de artıyor.Implement ettiğimiz interface'teki fonksiyonları override etme zorunluluğunuz olduğunu biliyorsunuzdur.İşte bu yüzden bu interface'teki fonksiyonlar override ettik.Bu sınıfın provider olduğu açıklanmış ve @Produces ve @Consumes ile girdi çıktı işlemlerinin formatları belirlenmiş.Override edilmiş fonksiyonlarda okuma,yazma ve kontrol işlemlerini yaptık.Burada javanın input/output sınıfından yararlandık.Client sınıfımızda ise get,put ve delete metodlarımızı deneme amaçlı 3 fonksiyon yazdık.Burada dikkat edilmesi gereken
yer ise .register(new YamlEntityProvider<>()).Sağlayıcımızı kaydettik,gibi düşünebilirsiniz.Çıktımız ise aşağıda.
Bu yazımın devamı gelecektir.İyi çalışmalar...
Hiç yorum yok:
Yorum Gönder