工作中涉及到Restful

Restful客户端
1.Struts定义Action时候引入Restful访问路径,加载数据

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public String loadLineNames() throws Exception {
String host = "10.180.1.24";
String database = "mesr_e43";
String schema = "mesr";
String resourceName = "MfgLine";
String str = AccessRestProxy.getJsonByPathParam(host, database, schema,
resourceName);
if (str != null && !"".equals(str)) {
Gson gson = new Gson();
mfgLines = gson.fromJson(str, new TypeToken<List<String>>() {
}.getType());
} else
mfgLines = new ArrayList<String>();
return SUCCESS;
}

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
public String loadStatusPageDatas() throws Exception {
try {
// step0 add configuration
String input = "input value like json String";
String resourcesName = "PreferenceConfig/addPreferenceConfig";
configData = AccessRestProxy
.postJsontoRestful(resourcesName, input);
// step1 加载preference配置信息 为了读取work order
Layout layout = LayoutLocalServiceUtil.getLayout(groupId, false,
layoutId);
PortletPreferences prefs = (PortletPreferences) PortletPreferencesFactoryUtil
.getLayoutPortletSetup(layout, portletId);
perferenceConfig = lineStatusMonitorService
.loadStatusPerferenceConfig(prefs);
// step2 写入第二行数据(假数据) 对应4条配置
String configresourceName = "LineStatusMonitor/BasicConfig";
String str = AccessRestProxy.getJsonByKey(configresourceName);
if (str != null && !"".equals(str)) {
Gson gson1 = new Gson();
row2config = gson1.fromJson(str,
new TypeToken<List<BasicConfig>>() {
}.getType());
} else {
row2config = new ArrayList<BasicConfig>();
}
// step3 从server端加载页面数据pageData
String resourceName ="PageData/SPC_WAR_spcportlet_INSTANCE_pzC0";
String strJson = AccessRestProxy.getJsonByKey(resourceName);
if (strJson != null) {
if (strJson.indexOf("returnFlag") == -1) {
Gson gson = new Gson();
pageData = gson.fromJson(strJson,
new TypeToken<List<List<AreaDisplayData>>>() {
}.getType());
}
}
} catch (Exception e) {
logger.error(e);
e.printStackTrace();
}
return SUCCESS;
}

2.访问Restful服务器端的设置

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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package com.sanmina.cdc.dashboard.linestatusmonitor.util;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Scanner;
public class AccessRestProxy {
private static String REST_SERVER_RESOUCE_PATH = "http://localhost:9080/mes-dashboard-server/rest/resources/";
public static String getJsonByPathParam(String mesrHost,
String mesrDatabase, String schema, String resourcesName) {
String rtv = "";
HttpURLConnection conn = null;
try {
URL url = new URL(REST_SERVER_RESOUCE_PATH
+ "10.180.1.24/mesr_e43/mesr/" + resourcesName);
conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setRequestMethod("GET");
conn.setRequestProperty("Content-Type", "application/json");
Scanner scanner;
if (conn.getResponseCode() != 200) {
scanner = new Scanner(conn.getErrorStream());
} else {
scanner = new Scanner(conn.getInputStream());
}
scanner.useDelimiter("\\Z");
while (scanner.hasNext())
rtv = scanner.next();
scanner.close();
} catch (Exception e) {
e.printStackTrace();
System.out.println("url:" + REST_SERVER_RESOUCE_PATH
+ "10.180.1.24/mesr_e43/mesr/" + resourcesName);
} finally {
if (conn != null)
conn.disconnect();
}
return rtv;
}
public static String getJsonByKey(String resourcesName) {
String rtv = "";
HttpURLConnection conn = null;
try {
URL url = new URL(REST_SERVER_RESOUCE_PATH + resourcesName);
conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setRequestMethod("GET");
conn.setRequestProperty("Content-Type", "application/json");
Scanner scanner;
if (conn.getResponseCode() != 200) {
scanner = new Scanner(conn.getErrorStream()); //nullpointer
} else {
scanner = new Scanner(conn.getInputStream());
}
scanner.useDelimiter("\\Z");
while (scanner.hasNext())
rtv = scanner.next();
scanner.close();
} catch (Exception e) {
e.printStackTrace();
System.out.println("url:" + REST_SERVER_RESOUCE_PATH
+ "10.180.1.24/mesr_e43/mesr/" + resourcesName);
} finally {
if (conn != null)
conn.disconnect();
}
return rtv;
}
public static String postJsontoRestful(String resourcesName,
String strJsonData) {
String rtv = "";
HttpURLConnection conn = null;
try {
URL url = new URL(REST_SERVER_RESOUCE_PATH + resourcesName);
conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/json");
String input = strJsonData; // "{\"Student\":{\"name\":\"Tom\"}}";
OutputStream os = conn.getOutputStream();
os.write(input.getBytes());
os.flush();
Scanner scanner;
if (conn.getResponseCode() != 200) {
scanner = new Scanner(conn.getErrorStream());
} else {
scanner = new Scanner(conn.getInputStream());
}
scanner.useDelimiter("\\Z");
while (scanner.hasNext())
rtv = scanner.next();
scanner.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (conn != null)
conn.disconnect();
}
/*返回的是action里面传入的preferenceConfig信息PreferenceConfigData,
含String portletId和List<OneTabConfig>*/
return rtv;
}
}

3.服务器端web.xml添加配置

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
<servlet>
<display-name>CXFNonSpringJaxrsServlet</display-name>
<servlet-name>CXFNonSpringJaxrsServlet</servlet-name>
<servlet-class>org.apache.cxf.jaxrs.servlet.CXFNonSpringJaxrsServlet </servlet-class>
<init-param>
<param-name>jaxrs.serviceClasses</param-name>
<param-value>
com.sanmina.cdc.dashboard.server.restful.LineStatusMonitorRestful,
com.sanmina.cdc.dashboard.server.restful.LineStatusMonitorCacheRestful,
com.sanmina.cdc.dashboard.server.restful.ProductLineRestful,
com.sanmina.cdc.dashboard.server.restful.MesrRestful
</param-value>
</init-param>
<init-param>
<param-name>jaxrs.providers</param-name>
<param-value>org.apache.cxf.jaxrs.provider.json.JSONProvider</param-value>
<!-- <param-value>org.apache.cxf.jaxrs.provider.JSONProvider</param-value> -->
</init-param>
<init-param>
<param-name>jaxrs.address</param-name>
<param-value>/resources</param-value>
</init-param>
<init-param>
<param-name>jaxrs.features</param-name>
<param-value>org.apache.cxf.jaxrs.swagger.Swagger2Feature(basePath=/mes-dashboard-server/rest/resources)</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>CXFNonSpringJaxrsServlet</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>

4.服务器端Restful API路径接口

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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package com.sanmina.cdc.dashboard.server.restful;
import java.util.List;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import com.google.gson.Gson;
import com.sanmina.cdc.dashboard.server.ehcache.LSMCacheServiceImpl;
import com.sanmina.cdc.dashboard.server.model.BasicConfig;
import com.sanmina.cdc.dashboard.server.model.PageData;
import com.sanmina.cdc.dashboard.server.model.PreferenceConfigData;
import com.sanmina.cdc.dashboard.server.model.Workstation;
import com.sanmina.cdc.dashboard.server.service.LineStatusMonitorService;
import com.sanmina.cdc.dashboard.server.servlet.ServerApplicationContext;
import com.sanmina.cdc.dashboard.server.util.Cache;
import io.swagger.annotations.Api;
@Consumes("application/json")
@Produces("application/json")
@Api(value = "/lineStatusMonitor")
public class LineStatusMonitorRestful {
@GET
@Path("/PageData/{portletId}")
// @ApiOperation(
// value = "Get operation with Response and @Default value",
// notes = "Get operation with Response and @Default value",
// response = OneTabConfig.class,
// responseContainer = "List"
// )
public Response loadPageDataByPreference(
// @ApiParam(value = "Page to fetch", required = true)
@PathParam("portletId") String portletId) {
if (portletId != null && !"".equals(portletId)) {
LineStatusMonitorService lineStatusMonitorService = (LineStatusMonitorService)
ServerApplicationContext
.getApplicationContext()
.getBean("lineStatusMonitorService");
PreferenceConfigData config = Cache.getPreferenceConfig(portletId);
if (config != null) {
Gson gson = new Gson();
PageData data = lineStatusMonitorService
.loadPageDataByPreference(config);
return Response.ok(gson.toJson(data.getPageDatas())).build();
} else {
return Response
.ok("{\"returnFlag\":\"false\",\"message\":\"can't get preferenceConfigData\"}")
.build();
}
} else {
return Response
.ok("{\"returnFlag\":\"false\",\"message\":\"please check portletId\"}")
.build();
}
}
@POST
@Path("/PreferenceConfig/addPreferenceConfig")
public Response addPreferenceConfigData(PreferenceConfigData config) {
if (config != null){
Cache.putPreferenceConfig(config.getPortletId(), config);
}
Gson gson = new Gson();
return Response.ok(gson.toJson(config)).build();
}
@GET
@Path("/PreferenceConfig/{portletId}")
public Response getPageDataByPreference(
@PathParam("portletId") String portletId) {
if (portletId != null) {
PreferenceConfigData config = Cache.getPreferenceConfig(portletId);
if (config != null) {
Gson gson = new Gson();
return Response.ok(gson.toJson(config)).build();
} else {
return Response
.ok("{\"returnFlag\":\"false\",\"message\":\"can't get configuration\"}")
.build();
}
} else {
return Response
.ok("{\"returnFlag\":\"false\",\"message:\":\"porletId can't empty \"}")
.build();
}
}
@POST
@Path("/PreferenceConfig/updatePreferenceConfig")
public Response updatePreferenceConfigData(PreferenceConfigData config) {
if (config != null)
Cache.putPreferenceConfig(config.getPortletId(), config);
Gson gson = new Gson();
return Response.ok(
"{\"returnFlag\":\"true\"," + gson.toJson(config) + "}")
.build();
}
@GET
@Path("/{databaseHost}/{database}/{schema}/partnumber")
@Produces(MediaType.APPLICATION_JSON)
public Response loadPartnumber(
@PathParam("databaseHost") String databaseHost,
@PathParam("database") String database,
@PathParam("schema") String schema) {
LineStatusMonitorService lineStatusMonitorService = (LineStatusMonitorService)
ServerApplicationContext
.getApplicationContext().getBean("lineStatusMonitorService");
List<String> ps = lineStatusMonitorService.loadPartnumber(databaseHost,
database, schema);
Gson gson = new Gson();
return Response.ok(gson.toJson(ps)).build();
}
@GET
@Path("/{databaseHost}/{database}/{schema}/process")
@Produces(MediaType.APPLICATION_JSON)
public Response loadProcess(@PathParam("databaseHost") String databaseHost,
@PathParam("database") String database,
@PathParam("schema") String schema) {
LineStatusMonitorService lineStatusMonitorService = (LineStatusMonitorService)
ServerApplicationContext
.getApplicationContext().getBean("lineStatusMonitorService");
List<String> ps = lineStatusMonitorService.loadProcess(databaseHost,
database, schema);
Gson gson = new Gson();
return Response.ok(gson.toJson(ps)).build();
}
@GET
@Path("/{databaseHost}/{database}/{schema}/MfgLine")
@Produces(MediaType.APPLICATION_JSON)
public Response loadMfgLine(@PathParam("databaseHost") String databaseHost,
@PathParam("database") String database,
@PathParam("schema") String schema) {
LineStatusMonitorService lineStatusMonitorService = (LineStatusMonitorService)
ServerApplicationContext
.getApplicationContext().getBean("lineStatusMonitorService");
List<String> ps = lineStatusMonitorService.loadMFGLine(databaseHost,
database, schema);
Gson gson = new Gson();
return Response.ok(gson.toJson(ps)).build();
}
@GET
@Path("/{databaseHost}/{database}/{schema}/workstation")
@Produces(MediaType.APPLICATION_JSON)
public Response loadWorkstation(
@PathParam("databaseHost") String databaseHost,
@PathParam("database") String database,
@PathParam("schema") String schema) {
LineStatusMonitorService lineStatusMonitorService = (LineStatusMonitorService)
ServerApplicationContext
.getApplicationContext().getBean("lineStatusMonitorService");
List<Workstation> ps = lineStatusMonitorService.loadWorkstation(
databaseHost, database, schema);
Gson gson = new Gson();
return Response.ok(gson.toJson(ps)).build();
}
@GET
@Path("/{databaseHost}/{database}/{schema}/workstation/{process}")
@Produces(MediaType.APPLICATION_JSON)
public Response loadFilterWorkstation(
@PathParam("databaseHost") String databaseHost,
@PathParam("database") String database,
@PathParam("schema") String schema,
@PathParam("process") String process) {
LineStatusMonitorService lineStatusMonitorService = (LineStatusMonitorService)
ServerApplicationContext
.getApplicationContext().getBean("lineStatusMonitorService");
List<Workstation> ps = lineStatusMonitorService
.loadWorkstationWithFilter(databaseHost, database, schema,
process);
Gson gson = new Gson();
// System.out.print("resuls 147:"+ gson.toJson(ps) );
return Response.ok(gson.toJson(ps)).build();
}
@GET
@Path("/Line/{workorder}")
@Produces(MediaType.APPLICATION_JSON)
public Response loadWorkOrder() {
LineStatusMonitorService lineStatusMonitorService = (LineStatusMonitorService)
ServerApplicationContext
.getApplicationContext().getBean("lineStatusMonitorService");
Gson gson = new Gson();
List<String> data = lineStatusMonitorService.loadWorkOrder();
return Response.ok(gson.toJson(data)).build();
}
@GET
@Path("/Dashboard/{BasicConfig}")
@Produces(MediaType.APPLICATION_JSON)
public Response loadBasicConfig() {
LineStatusMonitorService lineStatusMonitorService = (LineStatusMonitorService)
ServerApplicationContext
.getApplicationContext().getBean("lineStatusMonitorService");
Gson gson = new Gson();
List<BasicConfig> data = lineStatusMonitorService.loadRow2Config();
return Response.ok(gson.toJson(data)).build();
}
}