Close

JAX-RS - Application Examples

JAX-RS JAVA EE 


javax.ws.rs.core.Application provides one or more resources by overriding getClasses or getSingletons methods. We can skip overriding these methods, in that case the implementation can be used to only provide a root resource path by using @ApplicationPath. Also, in that case, the resources are discovered via classpath scanning for @Path and @Provider annotations.


package com.logicbig.example;

import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
import java.util.HashSet;
import java.util.Set;

@ApplicationPath("/")
public class MyRestApp 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());
set.add(new MySingletonResource2());
return set;
}
}
Original Post




@ApplicationPath("/")
public class MyApp extends Application {

@Override
public Map<String, Object> getProperties() {
Map<String, Object> map = new HashMap<>();
map.put("KEY_MAX_ORDER_LIMIT", "100");
return map;
}
}
@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();
}
}
Original Post




See Also