Skip to content

Commit 4924a47

Browse files
committed
i18n and filter using mapper and json
1 parent ffccebf commit 4924a47

28 files changed

+619
-78
lines changed

employee-mgr/pom.xml

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
34
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
45
<modelVersion>4.0.0</modelVersion>
56
<parent>
67
<groupId>org.springframework.boot</groupId>
78
<artifactId>spring-boot-starter-parent</artifactId>
89
<version>2.3.4.RELEASE</version>
9-
<relativePath/> <!-- lookup parent from repository -->
10+
<relativePath /> <!-- lookup parent from repository -->
1011
</parent>
1112
<groupId>com.smallintro</groupId>
1213
<artifactId>employee-mgr</artifactId>
@@ -16,6 +17,7 @@
1617

1718
<properties>
1819
<java.version>1.8</java.version>
20+
<org.mapstruct.version>1.3.1.Final</org.mapstruct.version>
1921
</properties>
2022

2123
<dependencies>
@@ -73,15 +75,40 @@
7375
<groupId>org.springframework.boot</groupId>
7476
<artifactId>spring-boot-starter-hateoas</artifactId>
7577
</dependency>
78+
<dependency>
79+
<groupId>org.modelmapper</groupId>
80+
<artifactId>modelmapper</artifactId>
81+
<version>2.3.8</version>
82+
</dependency>
83+
<dependency>
84+
<groupId>org.mapstruct</groupId>
85+
<artifactId>mapstruct-jdk8</artifactId>
86+
<version>${org.mapstruct.version}</version>
87+
</dependency>
7688
</dependencies>
7789

7890
<build>
7991
<plugins>
92+
<plugin>
93+
<groupId>org.apache.maven.plugins</groupId>
94+
<artifactId>maven-compiler-plugin</artifactId>
95+
<configuration>
96+
<source>${java.version}</source>
97+
<target>${java.version}</target>
98+
<annotationProcessorPaths>
99+
<path>
100+
<groupId>org.mapstruct</groupId>
101+
<artifactId>mapstruct-processor</artifactId>
102+
<version>${org.mapstruct.version}</version>
103+
</path>
104+
</annotationProcessorPaths>
105+
</configuration>
106+
</plugin>
80107
<plugin>
81108
<groupId>org.springframework.boot</groupId>
82109
<artifactId>spring-boot-maven-plugin</artifactId>
83110
</plugin>
84111
</plugins>
85112
</build>
86113

87-
</project>
114+
</project>

employee-mgr/src/main/java/com/smallintro/springboot/EmployeeMgrApplication.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
package com.smallintro.springboot;
22

3+
import java.util.Locale;
4+
35
import org.springframework.boot.SpringApplication;
46
import org.springframework.boot.autoconfigure.SpringBootApplication;
7+
import org.springframework.context.annotation.Bean;
8+
import org.springframework.context.support.ResourceBundleMessageSource;
9+
import org.springframework.web.servlet.LocaleResolver;
10+
import org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver;
511

612
@SpringBootApplication
713
public class EmployeeMgrApplication {
@@ -10,4 +16,20 @@ public static void main(String[] args) {
1016
SpringApplication.run(EmployeeMgrApplication.class, args);
1117
}
1218

19+
@Bean
20+
public LocaleResolver localResolver() {
21+
AcceptHeaderLocaleResolver localeResolver = new AcceptHeaderLocaleResolver();
22+
23+
localeResolver.setDefaultLocale(Locale.US);
24+
return localeResolver;
25+
}
26+
27+
@Bean
28+
public ResourceBundleMessageSource messageSource() {
29+
30+
ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
31+
32+
messageSource.setBasename("messages");
33+
return messageSource;
34+
}
1335
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.smallintro.springboot.config;
2+
3+
import org.modelmapper.ModelMapper;
4+
import org.springframework.context.annotation.Bean;
5+
import org.springframework.context.annotation.Configuration;
6+
7+
@Configuration
8+
public class AppclicationConfig {
9+
10+
@Bean
11+
public ModelMapper modelMapper() {
12+
return new ModelMapper();
13+
}
14+
15+
}
Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,23 @@
11
package com.smallintro.springboot.controller;
22

3+
import org.springframework.beans.factory.annotation.Autowired;
34
import org.springframework.web.bind.annotation.GetMapping;
5+
import org.springframework.web.bind.annotation.RequestHeader;
46
import org.springframework.web.bind.annotation.RequestMapping;
57
import org.springframework.web.bind.annotation.RestController;
68

9+
import com.smallintro.springboot.utils.ApplicationUtils;
10+
711
@RestController()
812
@RequestMapping("hello")
913
public class DefaultController {
10-
14+
15+
@Autowired
16+
ApplicationUtils appUtils;
17+
1118
@GetMapping
12-
public String getHealthStatus() {
13-
return "Hello World!!, I am Spring Boot Controller";
19+
public String getHealthStatus(@RequestHeader(name = "Accept-Language", required = false) String locale) {
20+
return appUtils.getI18nMessage("message.hello.world", locale);
1421
}
1522

1623
}

employee-mgr/src/main/java/com/smallintro/springboot/controller/EmployeeController.java

Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import javax.validation.constraints.Min;
66

77
import org.springframework.beans.factory.annotation.Autowired;
8+
import org.springframework.http.HttpStatus;
89
import org.springframework.http.MediaType;
910
import org.springframework.web.bind.annotation.DeleteMapping;
1011
import org.springframework.web.bind.annotation.GetMapping;
@@ -14,13 +15,18 @@
1415
import org.springframework.web.bind.annotation.RequestBody;
1516
import org.springframework.web.bind.annotation.RequestMapping;
1617
import org.springframework.web.bind.annotation.RestController;
18+
import org.springframework.web.server.ResponseStatusException;
1719

1820
import com.smallintro.springboot.entity.Employee;
21+
import com.smallintro.springboot.entity.EmployeeMmDto;
22+
import com.smallintro.springboot.entity.EmployeeMsDto;
23+
import com.smallintro.springboot.exception.RecordAlreadyExistsException;
24+
import com.smallintro.springboot.exception.RecordNotFoundException;
1925
import com.smallintro.springboot.service.EmployeeService;
2026

2127
import io.swagger.annotations.Api;
2228

23-
@Api(tags="Employee management APIs",value="Employee Controller")
29+
@Api(tags = "Employee management APIs", value = "Employee Controller")
2430
@RestController
2531
@RequestMapping("employee")
2632
public class EmployeeController {
@@ -33,26 +39,48 @@ public List<Employee> getEmployees() {
3339
return empService.getEmployees();
3440
}
3541

36-
@GetMapping(value = "/{empId}",produces = MediaType.APPLICATION_JSON_VALUE)
37-
public Employee getEmpInfo(@PathVariable String empId) {
38-
return empService.getEmpInfo(empId);
42+
@GetMapping(value = "/{empId}", produces = MediaType.APPLICATION_JSON_VALUE)
43+
public Employee getEmpInfoById(@PathVariable String empId) {
44+
return empService.getEmpInfoById(empId);
3945
}
4046

41-
@PostMapping(produces = MediaType.APPLICATION_JSON_VALUE)
42-
public String addEmployee(@RequestBody Employee emp) {
47+
@GetMapping(value = "/{empId}/modelmapper", produces = MediaType.APPLICATION_JSON_VALUE)
48+
public EmployeeMmDto getEmpInfoModelMapper(@PathVariable String empId) {
49+
try {
50+
return empService.getEmpInfoModelMapper(empId);
51+
} catch (RecordNotFoundException e) {
52+
throw new ResponseStatusException(HttpStatus.NOT_FOUND, e.getMessage());
53+
}
54+
}
4355

56+
@GetMapping(value = "/{empId}/mapstruct", produces = MediaType.APPLICATION_JSON_VALUE)
57+
public EmployeeMsDto getEmpInfoMapStruct(@PathVariable String empId) {
58+
try {
59+
return empService.getEmpInfoMapStruct(empId);
60+
} catch (RecordNotFoundException e) {
61+
throw new ResponseStatusException(HttpStatus.NOT_FOUND, e.getMessage());
62+
}
63+
}
64+
65+
@PostMapping(produces = MediaType.APPLICATION_JSON_VALUE)
66+
public Employee addEmployee(@RequestBody Employee emp) {
67+
68+
try {
4469
return empService.addEmployee(emp);
70+
} catch (RecordAlreadyExistsException e) {
71+
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, e.getMessage());
72+
}
4573

4674
}
4775

48-
@PutMapping(value="/{empId}",produces = MediaType.APPLICATION_JSON_VALUE)
49-
public String updateEmployee(@PathVariable Integer empId,@RequestBody Employee emp) {
50-
return empService.updateEmployee(empId, emp);
76+
@PutMapping(value = "/{empId}", produces = MediaType.APPLICATION_JSON_VALUE)
77+
public String updateEmployee(@PathVariable String empId, @RequestBody Employee emp) {
78+
return empService.updateEmployee(empId, emp);
5179
}
5280

53-
@DeleteMapping(value="/{empId}")
54-
public String delEmployee(@Min(1) @PathVariable Integer empId) {
55-
return empService.delEmployee(empId);
81+
@DeleteMapping(value = "/{empId}")
82+
public String delEmployee(@Min(1) @PathVariable String empId) {
83+
return empService.delEmployee(empId);
5684
}
5785

5886
}

employee-mgr/src/main/java/com/smallintro/springboot/controller/TechController.java

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,22 @@
11
package com.smallintro.springboot.controller;
22

33
import java.util.List;
4+
import java.util.Set;
5+
6+
import javax.validation.constraints.Min;
47

58
import org.springframework.beans.factory.annotation.Autowired;
69
import org.springframework.http.MediaType;
10+
import org.springframework.http.converter.json.MappingJacksonValue;
11+
import org.springframework.validation.annotation.Validated;
712
import org.springframework.web.bind.annotation.DeleteMapping;
813
import org.springframework.web.bind.annotation.GetMapping;
914
import org.springframework.web.bind.annotation.PathVariable;
1015
import org.springframework.web.bind.annotation.PostMapping;
1116
import org.springframework.web.bind.annotation.PutMapping;
1217
import org.springframework.web.bind.annotation.RequestBody;
1318
import org.springframework.web.bind.annotation.RequestMapping;
19+
import org.springframework.web.bind.annotation.RequestParam;
1420
import org.springframework.web.bind.annotation.RestController;
1521

1622
import com.smallintro.springboot.entity.Technology;
@@ -23,23 +29,31 @@
2329
@Api(tags = "Technology management APIs", value = "Technology Controller")
2430
@RestController
2531
@RequestMapping("technology")
32+
@Validated
2633
public class TechController {
2734

2835
@Autowired
2936
TechService techService;
3037

3138
@GetMapping(produces = MediaType.APPLICATION_JSON_VALUE)
32-
public List<Technology> getTechnologys() {
33-
return techService.getTechnologys();
39+
public List<Technology> getTechnologies() {
40+
return techService.getTechnologies();
3441
}
3542

3643
@GetMapping(value = "/{techName}", produces = MediaType.APPLICATION_JSON_VALUE)
37-
public Technology getTechnologyInfo(@ApiParam(value = "Technology name") @PathVariable String techName) {
38-
return techService.getTechnologyInfo(techName);
44+
public MappingJacksonValue getTechnologyByName(@ApiParam(value = "Technology name") @PathVariable String techName,
45+
@RequestParam Set<String> fields) {
46+
return techService.getTechnologyByName(techName, fields);
47+
}
48+
49+
@GetMapping(value = "/id/{techId}", produces = MediaType.APPLICATION_JSON_VALUE)
50+
public MappingJacksonValue getTechnologyById(@Min(1) @PathVariable Integer techId) {
51+
52+
return techService.getTechnologyById(techId);
3953
}
4054

4155
@PostMapping(produces = MediaType.APPLICATION_JSON_VALUE)
42-
public String addTech(@RequestBody List<Technology> techList) {
56+
public List<Technology> addTech(@RequestBody List<Technology> techList) {
4357
return techService.addTech(techList);
4458

4559
}

employee-mgr/src/main/java/com/smallintro/springboot/entity/Employee.java

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,22 @@
1717
@ApiModel("This model is for employee information")
1818
@Entity
1919
@Table(name = "tb_employee")
20-
public class Employee extends RepresentationModel<Employee>{
20+
public class Employee extends RepresentationModel<Employee> {
2121

2222
@ApiModelProperty(notes = "Employee ID should have first name initial+ Gender initial + YYYYMMDDHHMM", required = true, example = "SM201104180925", position = 1)
2323
@Id
2424
private String empId;
2525
private String empName;
2626
private Integer projectCode;
27-
private String eMailId;
27+
private String emailId;
28+
private String role;
2829
private Date joiningDate;
2930

3031
@ManyToMany(mappedBy = "employees")
3132
private List<Technology> technologies = new ArrayList<Technology>();
3233

34+
private String mgrId;
35+
3336
public String getEmpId() {
3437
return empId;
3538
}
@@ -54,12 +57,20 @@ public void setProjectCode(Integer projectCode) {
5457
this.projectCode = projectCode;
5558
}
5659

57-
public String geteMailId() {
58-
return eMailId;
60+
public String getEmailId() {
61+
return emailId;
62+
}
63+
64+
public void setEmailId(String emailId) {
65+
this.emailId = emailId;
66+
}
67+
68+
public String getRole() {
69+
return role;
5970
}
6071

61-
public void seteMailId(String eMailId) {
62-
this.eMailId = eMailId;
72+
public void setRole(String role) {
73+
this.role = role;
6374
}
6475

6576
public Date getJoiningDate() {
@@ -70,6 +81,14 @@ public void setJoiningDate(Date joiningDate) {
7081
this.joiningDate = joiningDate;
7182
}
7283

84+
public String getMgrId() {
85+
return mgrId;
86+
}
87+
88+
public void setMgrId(String mgrId) {
89+
this.mgrId = mgrId;
90+
}
91+
7392
public List<Technology> getTechnologies() {
7493
return technologies;
7594
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.smallintro.springboot.entity;
2+
3+
public class EmployeeMmDto {
4+
5+
private String empId;
6+
private String empName;
7+
private String emailId;
8+
9+
public String getEmpId() {
10+
return empId;
11+
}
12+
13+
public void setEmpId(String empId) {
14+
this.empId = empId;
15+
}
16+
17+
public String getEmpName() {
18+
return empName;
19+
}
20+
21+
public void setEmpName(String empName) {
22+
this.empName = empName;
23+
}
24+
25+
public String getEmailId() {
26+
return emailId;
27+
}
28+
29+
public void setEmailId(String emailId) {
30+
this.emailId = emailId;
31+
}
32+
33+
}

0 commit comments

Comments
 (0)