Skip to content

Commit a878e77

Browse files
author
borysn
committed
upgrade packages. better json response example
1 parent e961400 commit a878e77

File tree

7 files changed

+59
-22
lines changed

7 files changed

+59
-22
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package io.abnd.model;
2+
3+
public class Message {
4+
5+
public String title;
6+
public String message;
7+
8+
public Message(String title, String message) {
9+
this.title = title;
10+
this.message = message;
11+
}
12+
13+
public String getTitle() {
14+
return this.title;
15+
}
16+
17+
public String getMessage() {
18+
return this.message;
19+
}
20+
21+
}
Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,23 @@
11
package io.abnd.rest;
22

3+
import io.abnd.model.Message;
34
import org.springframework.beans.factory.annotation.Autowired;
4-
import org.springframework.web.bind.annotation.RequestMapping;
5-
import org.springframework.web.bind.annotation.RequestMethod;
6-
import org.springframework.web.bind.annotation.RestController;
5+
import org.springframework.web.bind.annotation.*;
76

87
import com.google.gson.JsonObject;
98

109
import io.abnd.service.intf.TestService;
1110

11+
import java.util.List;
12+
1213
@RestController
1314
public class TestController {
1415

1516
@Autowired
1617
private TestService testService;
1718

1819
@RequestMapping(value="/test/get/json", method=RequestMethod.GET, produces="application/json")
19-
public String testGetJson() {
20-
JsonObject jsonObject = new JsonObject();
21-
JsonObject message = new JsonObject();
22-
23-
String result = testService.test();
24-
25-
message.addProperty("message", result);
26-
jsonObject.add("test", message);
27-
28-
return jsonObject.toString();
20+
public @ResponseBody List<Message> testGetJson() {
21+
return this.testService.test();
2922
}
3023
}
Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,23 @@
11
package io.abnd.service.impl;
22

3+
import io.abnd.model.Message;
34
import org.springframework.stereotype.Service;
45

56
import io.abnd.service.intf.TestService;
67

8+
import java.util.ArrayList;
9+
import java.util.List;
10+
711
@Service
812
public class TestServiceImpl implements TestService {
913

10-
public String test() {
11-
return "Hello, World!";
14+
public List<Message> test() {
15+
ArrayList<Message> messages = new ArrayList<Message>();
16+
17+
messages.add(new Message("Message1", "Hello, world!"));
18+
messages.add(new Message("Message2", "Another one!"));
19+
20+
return messages;
1221
}
1322

1423
}
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package io.abnd.service.intf;
22

3+
import java.util.List;
4+
5+
import io.abnd.model.Message;
6+
37
public interface TestService {
4-
public String test();
8+
public List<Message> test();
59
}

src/main/web/app/hello/hello.component.html

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,7 @@
33
</div>
44

55
<div><h3>JSON</h3><div>{{jsonResponse}}</div></div>
6-
<div><h3>message</h3><div>{{message}}</div></div>
6+
<div *ngFor="let message of messages">
7+
<h3>{{message.title}}</h3>
8+
<div>{{message.message}}</div>
9+
</div>

src/main/web/app/hello/hello.component.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ import {CORE_DIRECTIVES} from '@angular/common';
55
import {ROUTER_DIRECTIVES} from '@angular/router';
66
import {HelloService} from './hello.service';
77

8+
interface MessageJson {
9+
title: string;
10+
message: string;
11+
}
12+
813
@Component({
914
selector: 'test',
1015
templateUrl: 'app/hello/hello.component.html',
@@ -15,7 +20,7 @@ export class HelloComponent {
1520

1621
// vars
1722
private jsonResponse: string;
18-
private message: string;
23+
private messages: Array<MessageJson>;
1924
private subscription;
2025

2126
// constructor
@@ -26,8 +31,10 @@ export class HelloComponent {
2631
// save subscription
2732
this.subscription = this.helloService.getTest()
2833
.subscribe(
29-
(data) => {this.jsonResponse = JSON.stringify(data);
30-
this.message = data.test.message;},
34+
(data) => {
35+
this.jsonResponse = JSON.stringify(data);
36+
this.messages = data;
37+
},
3138
(err) => console.log(err),
3239
() => console.log('hello service test complete')
3340
);

src/main/web/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@
3838
},
3939
"devDependencies": {
4040
"concurrently": "^2.2.0",
41-
"del": "2.2.1",
41+
"del": "2.2.2",
4242
"gulp": "^3.9.1",
4343
"gulp-cli": "^1.2.2",
44-
"gulp-autoprefixer": "3.1.0",
44+
"gulp-autoprefixer": "3.1.1",
4545
"gulp-sass": "^2.3.2",
4646
"gulp-typescript": "2.13.6",
4747
"jasmine-core": "2.4.1",

0 commit comments

Comments
 (0)