-
Notifications
You must be signed in to change notification settings - Fork 1
/
GuiceJerseryJettyExample.java
64 lines (54 loc) · 1.75 KB
/
GuiceJerseryJettyExample.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package com.google.code.guice.example;
import com.google.code.guice.jsonconfig.JsonConfigModule;
import com.google.code.jersey.Jerseys;
import com.google.code.jersey.ServerConfig;
import com.google.code.jersey.jetty.JerseyJettyServer;
import com.google.code.jersey.jetty.JettyServerModule;
import com.google.inject.*;
import lombok.extern.slf4j.Slf4j;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import java.util.Properties;
/**
* Created by yihaibo on 2019-10-08.
* jersey restful简单列子
*/
@Slf4j
public class GuiceJerseryJettyExample {
public static void main(String[] args) throws Exception {
Injector injector = Guice.createInjector(new JsonConfigModule(), new JettyServerModule(), new Module() {
@Override
public void configure(Binder binder) {
Jerseys.addResource(binder, IndexResource.class);
}
@Provides @Singleton
Properties provideProperties() {
Properties props = new Properties();
props.put("server.http.host", "0.0.0.0");
props.put("server.http.port", "9000");
return props;
}
});
JerseyJettyServer jerseyJettyServer = injector.getInstance(JerseyJettyServer.class);
jerseyJettyServer.start();
Thread.currentThread().join();
}
@Singleton
@Path("/index")
public static class IndexResource {
private ServerConfig serverConfig;
@Inject
public IndexResource(ServerConfig serverConfig) {
this.serverConfig = serverConfig;
}
@GET
@Produces(MediaType.APPLICATION_JSON)
public ServerConfig doGet(@Context final HttpServletRequest req) {
return serverConfig;
}
}
}