-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathREST.txt
313 lines (219 loc) · 7.51 KB
/
REST.txt
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
1. REST client demo
Create a spring mvc application.
asual define config and controller is now replaced to REST controller
------------------------------------
2. already we created back end services with jar.
--------------------------------------
3. REST clien we need to incorp.
create lib folder
web-inf/lib
Add h2 and backendservices jar
and build the path
or
Maven dependency to add the jar
<dependency>
<groupId>com.niit</groupId>
<artifactId>ItemBackend</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
------------------
4.
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.5</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>5.2.16.Final</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/jstl/jstl -->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-dbcp2</artifactId>
<version>2.2.0</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.196</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>com.niit</groupId>
<artifactId>ItemBackend</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.5</version>
</dependency>
--------------------------------------------
5.
package com.niit.ItemRest.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
@Configuration
@EnableWebMvc
@ComponentScan(basePackages="com.niit")
public class AppConfig {
@Bean(name="viewResolver")
public ViewResolver getViewResolver() {
InternalResourceViewResolver viewResolver=new InternalResourceViewResolver();
viewResolver.setPrefix("/WEB-INF/views/");
viewResolver.setSuffix(".jsp");
return viewResolver;
}
}
----------------------------------------------------------------
6.
package com.niit.ItemRest.config;
import javax.servlet.ServletRegistration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
public class WebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getRootConfigClasses() {
// TODO Auto-generated method stub
// return null;
return new Class[] {AppConfig.class};
}
@Override
protected Class<?>[] getServletConfigClasses() {
// TODO Auto-generated method stub
return null;
}
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**");
}
@Override
protected String[] getServletMappings() {
// TODO Auto-generated method stub
//return null;
return new String[] {"/"};
}
@Override
protected void customizeRegistration(ServletRegistration.Dynamic registration)
{
registration.setInitParameter("dispatchOptionsRequest","true");
registration.setAsyncSupported(true);
}
}
--------------------------------------------------
7. // REST is not focused on UI
put by default
index.jsp
<html>
<body>
<h2>Hello World!</h2>
</body>
</html>
---------------------------------------------------
package com.niit.ItemRest.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import com.niit.ItemBackend.model.*;
import com.niit.ItemBackend.service.ItemService;
import org.springframework.web.bind.annotation.RequestMapping;
@RestController
@RequestMapping("/api/item")
@CrossOrigin(origins = "http://localhost:4200")
public class ItemController {
@Autowired
private ItemService itemService; // DI
@GetMapping
public List<Item> listAllItem() {
List<Item> item=itemService.findAllItems();//
//, HttpStatus.CREATED);
return item;
}
@GetMapping("/{itemId}")
public ResponseEntity<Item> getItem(@PathVariable("itemId") int itemId) {
if(itemService.findItemById(itemId)!=null) {
return new ResponseEntity<Item>(itemService.findItemById(itemId), HttpStatus.OK);
}
else
return new ResponseEntity<Item>(HttpStatus.NOT_FOUND);
}
@DeleteMapping("/{itemId}")
public ResponseEntity<Void> deleteItem(@PathVariable("itemId") int itemId) {
if(itemService.findItemById(itemId)!=null) {
itemService.deleteItem(itemId);
return new ResponseEntity<Void>(HttpStatus.OK);
}
else
return new ResponseEntity<Void>(HttpStatus.NOT_FOUND);
}
@PostMapping
public ResponseEntity<Void> addItem(@RequestBody Item item) {
if(itemService.findItemByName(item.getDescription())!=null) {
return new ResponseEntity<Void>(HttpStatus.CONFLICT);
}
else {
itemService.addItem(item);
return new ResponseEntity<Void>(HttpStatus.CREATED);
}
}
@PutMapping
public ResponseEntity<Void> updateItem(@RequestBody Item item) {
if(itemService.findItemById(item.getItemId())!=null) {
itemService.updateItem(item);
return new ResponseEntity<Void>(HttpStatus.OK);
}
else {
return new ResponseEntity<Void>(HttpStatus.NOT_FOUND);
}
}
}
----------------------------------