Ciao!
sto testato Apache CXF, ma riscontro sempre un problema.
questo il main:
import org.apache.cxf.endpoint.Server;
import org.apache.cxf.jaxrs.JAXRSServerFactoryBean;
import org.apache.cxf.jaxrs.lifecycle.SingletonResourceProvider;
public class Main {
public static void main(String[] args) {
try {
JAXRSServerFactoryBean factoryBean = new JAXRSServerFactoryBean();
[b]factoryBean.setResourceClasses(PersonaImpl.class);[/b]
factoryBean.setResourceProvider(new SingletonResourceProvider(new PersonaImpl()));
factoryBean.setAddress("http://localhost:8080/");
Server server = factoryBean.create();
System.out.println("Start del server");
Thread.sleep(60 * 1000);
System.out.println("Stop del server");
server.destroy();
System.exit(0);
} catch (InterruptedException e) {
System.out.println(e.getMessage());
}
}
}
ho segnato la riga con errore:
Exception in thread "main" java.lang.NoClassDefFoundError: javax/annotation/Resource
at org.apache.cxf.common.injection.ResourceInjector.<clinit>(ResourceInjector.java:59)
at org.apache.cxf.bus.extension.ExtensionManagerImpl.loadAndRegister(ExtensionManagerImpl.java:234)
at org.apache.cxf.bus.extension.ExtensionManagerImpl.activateAllByType(ExtensionManagerImpl.java:142)
at org.apache.cxf.bus.extension.ExtensionManagerBus.<init>(ExtensionManagerBus.java:149)
at org.apache.cxf.bus.extension.ExtensionManagerBus.<init>(ExtensionManagerBus.java:155)
at org.apache.cxf.bus.CXFBusFactory.createBus(CXFBusFactory.java:40)
at org.apache.cxf.bus.CXFBusFactory.createBus(CXFBusFactory.java:36)
at org.apache.cxf.bus.CXFBusFactory.createBus(CXFBusFactory.java:32)
at org.apache.cxf.BusFactory.getDefaultBus(BusFactory.java:107)
at org.apache.cxf.BusFactory.createThreadBus(BusFactory.java:243)
at org.apache.cxf.BusFactory.getThreadDefaultBus(BusFactory.java:227)
at org.apache.cxf.BusFactory.getThreadDefaultBus(BusFactory.java:214)
at org.apache.cxf.jaxrs.JAXRSServiceFactoryBean.getBus(JAXRSServiceFactoryBean.java:66)
at org.apache.cxf.jaxrs.JAXRSServiceFactoryBean.createResourceInfo(JAXRSServiceFactoryBean.java:203)
at org.apache.cxf.jaxrs.JAXRSServiceFactoryBean.setResourceClasses(JAXRSServiceFactoryBean.java:166)
at org.apache.cxf.jaxrs.JAXRSServiceFactoryBean.setResourceClasses(JAXRSServiceFactoryBean.java:212)
at org.apache.cxf.jaxrs.JAXRSServerFactoryBean.setResourceClasses(JAXRSServerFactoryBean.java:296)
at com.mp.test.Main.main(Main.java:12)
Caused by: java.lang.ClassNotFoundException: javax.annotation.Resource
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:583)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
... 18 more
questa è la classe PersonaImpl:
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import java.util.HashMap;
import java.util.Map;
@Path("persona")
@Produces("text/xml")
public class PersonaImpl {
private Map<Integer, Persona> persone = new HashMap<>();
{
Persona p1 = new Persona();
p1.setId(1);
p1.setNome("Matteo");
Persona p2 = new Persona();
p2.setId(2);
p2.setNome("Andrea");
persone.put(1, p1);
persone.put(2, p2);
}
@GET
@Path("persone/{pId}")
public Persona getPersona(@PathParam("pId") int pId) {
return findById(pId);
}
private Persona findById(int id) {
for (Map.Entry<Integer, Persona> map : persone.entrySet()) {
if (map.getKey() == id) {
return map.getValue();
}
}
return null;
}
}
qulche idea??