-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathResponseUtil.java
More file actions
59 lines (50 loc) · 2.09 KB
/
ResponseUtil.java
File metadata and controls
59 lines (50 loc) · 2.09 KB
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
package com.app.utils;
import com.app.dto.StandardResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.context.MessageSource;
import org.springframework.context.MessageSourceResolvable;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
@Component
@RequiredArgsConstructor
public class ResponseUtil {
private final MessageSource messageSource;
/**
* Creates a success response.
*
* @param responseCode The response code for success.
* @param responseObject The object to include in the response.
* @return A StandardResponse object.
*/
public <T> StandardResponse<T> createSuccessResponse(String responseCode, T responseObject, Object... args) {
return new StandardResponse<>(StringUtils.uncapitalizeAsProperty(responseCode), getMessage("success.description." + responseCode, args), responseObject, true);
}
/**
* Creates an error response.
*
* @param responseCode The response code for the error.
* @param errorDetails Optional error details to include in the response.
* @return A StandardResponse object.
*/
public <T> StandardResponse<T> createErrorResponse(String responseCode, T errorDetails, Object... args) {
return new StandardResponse<>(StringUtils.uncapitalizeAsProperty(responseCode), getMessage("error.description." + responseCode, args), errorDetails, false);
}
/**
* Retrieves a message from the MessageSource based on the current locale.
*
* @param key The message key.
* @return The localized message.
*/
public String getMessage(String key, Object... args) {
return messageSource.getMessage(key, args, LocaleContextHolder.getLocale());
}
/**
* Retrieves a message from the MessageSource based on the current locale.
*
* @return The localized message.
*/
public String getMessage(MessageSourceResolvable resolvable) {
return messageSource.getMessage(resolvable, LocaleContextHolder.getLocale());
}
}