UserController.java
@RestController
@Slf4j
@RequiredArgsConstructor
@RequestMapping("/")
public class UserController {
private final UserService userService;
private final Greeting greeting;
@GetMapping("/health_check")
public String status() {
return "It's working in User Service";
}
@GetMapping("/welcome")
public String welcome() {
return greeting.getMessage();
}
@PostMapping("/users")
public ResponseEntity createUser(@RequestBody RequestUser user) {
ModelMapper mapper = new ModelMapper();
mapper.getConfiguration().setMatchingStrategy(MatchingStrategies.STRICT);
UserDto userDto = mapper.map(user, UserDto.class);
userService.createUser(userDto);
ResponseUser responseUser = mapper.map(userDto, ResponseUser.class);
return ResponseEntity.status(HttpStatus.CREATED).body(responseUser);
}
}
UserDto.java
@Data
public class UserDto {
private String email;
private String name;
private String pwd;
private String userId;
private Date createdAt;
private String encryptedPwd;
}
UserEntity.java
@Data
@Entity
@Table(name = "testusers")
public class UserEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(nullable = false, length = 50, unique = true)
private String email;
@Column(nullable = false, length = 50)
private String name;
@Column(nullable = false, unique = true)
private String userId;
@Column(nullable = false, unique = true)
private String encryptedPwd;
}
UserRepository.java
public interface UserRepository extends CrudRepository<UserEntity, Long> {
}
RequestUser.java
@Data
public class RequestUser {
@NotNull(message = "Email cannot be null")
@Size(min = 2, message = "Email not be less than two characters")
private String email;
@NotNull(message = "Name cannot be null")
@Size(min = 2, message = "name not be less than two characters")
private String name;
@NotNull(message = "Password cannot be null")
@Size(min = 8, message = "Password must be equal or grater than 8 characters")
private String pwd;
}
ResponseUser.java
@Data
public class ResponseUser {
private String email;
private String name;
private String userId;
}
application.yml
server:
port: 0
spring:
application:
name: user-service
h2:
console:
enabled: true
settings:
web-allow-others: true
path: /h2-console
datasource:
driver-class-name: org.h2.Driver
url: jdbc:h2:mem:testdb
# username: wcadmin
# password: wcadmin1
eureka:
instance:
instance-id: ${spring.application.name}:${spring.application.instance_id:${random.value}}
client:
register-with-eureka: true
fetch-registry: true
service-url:
defaultZone: http://127.0.0.1:8761/eureka
greeting:
message: Welcome to the Simple E-commerce.