Skip to content

Commit c8f511c

Browse files
committed
sample Users controller
1 parent 18b68be commit c8f511c

File tree

3 files changed

+23
-6
lines changed

3 files changed

+23
-6
lines changed

readme.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ Spring:
2020
@EnableOAuth2Sso makes application an OAuth2 client
2121
############################################################################################################################################
2222
#################################################### INTERNAL OAUTH2 SECTION ###############################################################
23-
Application deploys on localhost:8080, context path is /
23+
Application deploying on localhost:8080, context path is /
2424
Therefore if we try to get resource /api/users we send HTTP GET on localhost:8080/api/users, but without Oauth2 token we getting
2525
401 Response with following JSON:
2626
{
@@ -82,6 +82,7 @@ You'll receive following JSON on userInfo request:
8282
{
8383
"sub": "mjolnir"
8484
}
85+
8586
###########################################################################################################################################
8687
############################################################## RESOURCES ##################################################################
8788
OAuth protocol description: https://tools.ietf.org/html/rfc6749#section-3

src/main/java/ru/techdemo/controllers/api/UsersController.java

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,29 @@
55
*/
66
package ru.techdemo.controllers.api;
77

8-
import org.springframework.web.bind.annotation.RequestMapping;
8+
import java.util.List;
9+
import org.springframework.beans.factory.annotation.Autowired;
10+
import org.springframework.web.bind.annotation.GetMapping;
11+
import org.springframework.web.bind.annotation.PathVariable;
912
import org.springframework.web.bind.annotation.RestController;
13+
import ru.techdemo.dal.IApplicationDataContext;
14+
import ru.techdemo.dal.entity.UserEntity;
1015

1116
@RestController
1217
public class UsersController {
1318

14-
// todo: umv: implement proiperly, get from context
15-
@RequestMapping("/api/users")
16-
public String get(){
17-
return "{\"userId\": \"0\", \"userName\": \"admin\"}";
19+
@GetMapping("/api/users")
20+
public List<UserEntity> get(){
21+
List<UserEntity> users = dataContext.getUsersRepository().getAll();
22+
return users;
1823
}
24+
25+
@GetMapping("/api/users/{id}")
26+
public UserEntity get(@PathVariable(required = true)Long id){
27+
UserEntity user = dataContext.getUsersRepository().getById(id);
28+
return user;
29+
}
30+
31+
@Autowired
32+
IApplicationDataContext dataContext;
1933
}

src/main/java/ru/techdemo/dal/InMemoryMockApplicationContext.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,14 @@
99
import java.util.Arrays;
1010
import java.util.List;
1111
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
12+
import org.springframework.stereotype.Service;
1213
import ru.techdemo.dal.entity.RoleEntity;
1314
import ru.techdemo.dal.entity.UserEntity;
1415
import ru.techdemo.dal.repository.IRepository;
1516
import ru.techdemo.dal.repository.MockRepository;
1617

1718

19+
@Service
1820
public class InMemoryMockApplicationContext implements IApplicationDataContext {
1921

2022
public InMemoryMockApplicationContext(){

0 commit comments

Comments
 (0)