1 |
package dk.thoerup.webservice; |
2 |
|
3 |
import java.util.Collections; |
4 |
import java.util.HashMap; |
5 |
import java.util.Map; |
6 |
import java.util.Set; |
7 |
|
8 |
import javax.ws.rs.ApplicationPath; |
9 |
import javax.ws.rs.core.Application; |
10 |
|
11 |
@ApplicationPath("rest") |
12 |
public class MyApplication extends Application { |
13 |
|
14 |
@Override |
15 |
public Set<Class<?>> getClasses() { |
16 |
|
17 |
Set<Class<?>> resources = new java.util.HashSet<>(); |
18 |
|
19 |
System.out.println("REST configuration starting: getClasses()"); |
20 |
|
21 |
//features |
22 |
//this will register MOXy JSON providers |
23 |
resources.add(org.glassfish.jersey.moxy.json.MoxyJsonFeature.class); |
24 |
//we could also use this |
25 |
resources.add(org.glassfish.jersey.moxy.xml.MoxyXmlFeature.class); |
26 |
|
27 |
//instead let's do it manually: |
28 |
resources.add(JsonMoxyConfigurationContextResolver.class); |
29 |
|
30 |
resources.add(ExceptionListener.class); |
31 |
|
32 |
|
33 |
resources.add(RestSimpleJerseyTest.class); |
34 |
//==> we could also choose packages, see below getProperties() |
35 |
|
36 |
System.out.println("REST configuration ended successfully."); |
37 |
|
38 |
return resources; |
39 |
} |
40 |
|
41 |
@Override |
42 |
public Set<Object> getSingletons() { |
43 |
return Collections.emptySet(); |
44 |
} |
45 |
|
46 |
@Override |
47 |
public Map<String, Object> getProperties() { |
48 |
System.out.println("Application->properties"); |
49 |
Map<String, Object> properties = new HashMap<>(); |
50 |
|
51 |
//in Jersey WADL generation is enabled by default, but we don't |
52 |
//want to expose too much information about our apis. |
53 |
//therefore we want to disable wadl (http://localhost:8080/service/application.wadl should return http 404) |
54 |
//see https://jersey.java.net/nonav/documentation/latest/user-guide.html#d0e9020 for details |
55 |
properties.put("jersey.config.server.wadl.disableWadl", true); |
56 |
|
57 |
//we could also use something like this instead of adding each of our resources |
58 |
//explicitely in getClasses(): |
59 |
//properties.put("jersey.config.server.provider.packages", "com.nabisoft.tutorials.mavenstruts.service"); |
60 |
|
61 |
|
62 |
return properties; |
63 |
} |
64 |
} |
65 |
|
66 |
/*public class MyApplication extends ResourceConfig { |
67 |
public MyApplication() { |
68 |
System.out.println("Loading MyApplication class"); |
69 |
|
70 |
//register(ApplicationEventListener.class); |
71 |
packages("dk.thoerup.webservice"); |
72 |
//register(RestSimpleJerseyTest.class); |
73 |
register(JsonMoxyConfigurationContextResolver.class); |
74 |
} |
75 |
|
76 |
|
77 |
}*/ |