Injecting Application
İlk olarak application'dan başlıyoruz.Burada ApplicationConfig classımızda extends ettiğimiz Application classındaki getProperties methodunu override ederek kaynak sınıfımıza enjekte edeceğiz.Bu sayede kaynak sınıflarımızı ApplicationConfig class'ımızdan kontrol edebiliriz.Şimdi bunu basitçe nasıl yapabileceğimize bakalım.
This file contains 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.HashMap; | |
import java.util.Map; | |
import javax.ws.rs.ApplicationPath; | |
import javax.ws.rs.core.Application; | |
@ApplicationPath("/") | |
public class ApplicationConfig extends Application { | |
@Override | |
public Map<String,Object> getProperties(){ | |
Map<String,Object> map=new HashMap<>(); | |
map.put("KEY_MAX_ORDER_LIMIT", "100"); | |
return map; | |
} | |
} |
This file contains 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; | |
import javax.ws.rs.core.Response; | |
public class OrderClient { | |
public static void main(String[] args) throws Exception { | |
Client client = ClientBuilder.newBuilder().build(); | |
WebTarget target = | |
client.target("http://localhost:8080/orders"); | |
Response response = target.request().get(Response.class); | |
String responseString = response.readEntity(String.class); | |
System.out.println("response: " + responseString); | |
} | |
} |
This file contains 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.Map; | |
import java.util.stream.Collectors; | |
import javax.ws.rs.GET; | |
import javax.ws.rs.Path; | |
import javax.ws.rs.core.Application; | |
import javax.ws.rs.core.Context; | |
@Path("/orders") | |
public class OrderResource { | |
@GET | |
public String getAllOrders(@Context Application app) { | |
Map<String, Object> properties = app.getProperties(); | |
String props = properties.entrySet().stream() | |
.map(e -> e.getKey() + "=" + e.getValue()) | |
.collect(Collectors.joining("\n")); | |
System.out.println(props); | |
return properties.get("KEY_MAX_ORDER_LIMIT").toString(); | |
} | |
} |
Injecting UriInfo
Aynı şekilde UriInfo arayüzümüzdeki fonksiyonların kullanımına ve bunların yine aynı şekilde server'ımızda tutulan log'una bakalım.
Injecting HttpHeaders
Umarım faydalı olmuştur.Bir sonraki yazımızda görüşmek üzere.