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 java.util.*; | |
import javax.ws.rs.*; | |
import javax.ws.rs.core.*; | |
@ApplicationPath("/") | |
public class ApplicationConfig extends Application { | |
@Override | |
public Set<Class<?>> getClasses(){ | |
Set<Class<?>> set =new HashSet<>(); | |
set.add(MyResource.class); | |
return set; | |
} | |
@Override | |
public Set<Object> getSingletons(){ | |
Set<Object> set=new HashSet<>(); | |
set.add(new MySingletonResource()); | |
return set; | |
} | |
} |
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.GET; | |
import javax.ws.rs.Path; | |
@Path("/") | |
public class MyResource { | |
private int counter; | |
@GET | |
@Path("count1") | |
public void count() { | |
counter++; | |
} | |
@GET | |
@Path("counter1") | |
public int getCounter() { | |
return counter; | |
} | |
@GET | |
@Path("reset1") | |
public void reset() { | |
counter = 0; | |
} | |
} |
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.GET; | |
import javax.ws.rs.Path; | |
@Path("/") | |
public class MySingletonResource { | |
private int counter; | |
@GET | |
@Path("count2") | |
public void count() { | |
counter++; | |
} | |
@GET | |
@Path("counter2") | |
public int getCounter() { | |
return counter; | |
} | |
@GET | |
@Path("reset2") | |
public void reset() { | |
counter = 0; | |
} | |
} |
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.client.Client; | |
import javax.ws.rs.client.ClientBuilder; | |
import javax.ws.rs.client.WebTarget; | |
public class MyClient1 { | |
public static void main(String[] args) { | |
getRequest("/reset1", Void.class); | |
countRequest("/count1"); | |
Integer counter = getRequest("/counter1", Integer.class); | |
System.out.printf("counter1: %s%n", counter); | |
getRequest("/reset2", Void.class); | |
countRequest("/count2"); | |
Integer counter2 = getRequest("/counter2", Integer.class); | |
System.out.printf("counter2: %s%n", counter2); | |
} | |
public static void countRequest(String uri) { | |
for (int i = 0; i < 10; i++) { | |
getRequest(uri, Void.class); | |
} | |
} | |
public static <T> T getRequest(String uri, Class<T> responseType) { | |
Client client = ClientBuilder.newClient(); | |
WebTarget target = client.target("http://localhost:8080" + uri); | |
return target.request().get(responseType); | |
} | |
} |
Gördüğünüz üzere iki farklı kaynak sınıfımız ve ApplicationConfig sınıfımız var.ApplicationConfig'te gördüğünüz set collection'ı,list'in bir benzeri olup verileri herhangi bir index ile tutmaz.İki fonksiyonumuzu override ettik.
counter1: 0
counter2: 10
Çıktımız bu şekilde olacaktır.Client classımızın yaptığı request mesajlarına karşılık ApplicationConfig class'ımızda ezdiğimiz fonksiyonlar :).<T> gelecek olan class'tan sonra şekil alacak.Bir sonraki yazımızda görüşmek üzere.
Hiç yorum yok:
Yorum Gönder