Update RestUtil

Pembuatan function get json response hanya untuk message label-success
This commit is contained in:
Salman Manoe 2022-01-14 07:59:32 +07:00
parent 6e03d4b0c6
commit 44b5c0ce4d

View File

@ -20,7 +20,6 @@ import com.google.gson.Gson;
public class RestUtil { public class RestUtil {
private static final String CONTENT_TYPE = "Content-Type"; private static final String CONTENT_TYPE = "Content-Type";
private static final String ACCESS_CONTROL_ALLOW_ORIGIN = "Access-Control-Allow-Origin";
/** /**
* get JSON response from Object * get JSON response from Object
@ -33,7 +32,6 @@ public class RestUtil {
public static <T> ResponseEntity<T> getJsonResponse(T src) { public static <T> ResponseEntity<T> getJsonResponse(T src) {
HttpHeaders headers = new HttpHeaders(); HttpHeaders headers = new HttpHeaders();
//headers.add(ACCESS_CONTROL_ALLOW_ORIGIN, "*");
headers.set(CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE); headers.set(CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
return new ResponseEntity<T>(src, headers, HttpStatus.OK); return new ResponseEntity<T>(src, headers, HttpStatus.OK);
} }
@ -42,14 +40,15 @@ public class RestUtil {
* get JSON response from Object with HTTP status * get JSON response from Object with HTTP status
* *
* @param src * @param src
* @param status
* @param <T> * @param <T>
* source class * source class
* @return @ResponseEntity * @return @ResponseEntity
*/ */
@SuppressWarnings({ "unchecked", "rawtypes" })
public static <T> ResponseEntity<T> getJsonResponse(T src, HttpStatus status) { public static <T> ResponseEntity<T> getJsonResponse(T src, HttpStatus status) {
HttpHeaders headers = new HttpHeaders(); HttpHeaders headers = new HttpHeaders();
// headers.add(ACCESS_CONTROL_ALLOW_ORIGIN, "*");
headers.set(CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE); headers.set(CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
Map<String, Object> map = new HashMap<String, Object>(); Map<String, Object> map = new HashMap<String, Object>();
map.put("data", src); map.put("data", src);
@ -64,9 +63,9 @@ public class RestUtil {
* @param <T> * @param <T>
* @return * @return
*/ */
public static <T> ResponseEntity<T> getJsonResponse(T src, @SuppressWarnings({ "unchecked", "rawtypes" })
HttpStatus status, Map<String, String> mapHeaderMessage) { public static <T> ResponseEntity<T> getJsonResponse(T src, HttpStatus status,
Map<String, String> mapHeaderMessage) {
HttpHeaders headers = new HttpHeaders(); HttpHeaders headers = new HttpHeaders();
@ -79,11 +78,27 @@ public class RestUtil {
Map<String, Object> map = new HashMap<String, Object>(); Map<String, Object> map = new HashMap<String, Object>();
map.put("messages", mapHeaderMessage); map.put("messages", mapHeaderMessage);
map.put("data", src); map.put("data", src);
//headers.add(ACCESS_CONTROL_ALLOW_ORIGIN, "*");
headers.set(CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE); headers.set(CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
return new ResponseEntity(map, headers, status); return new ResponseEntity(map, headers, status);
} }
/**
*
* @param status
* @param mapHeaderMessage
* @return
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
public static <T> ResponseEntity<T> getJsonResponse(HttpStatus status, Map<String, String> mapHeaderMessage) {
HttpHeaders headers = new HttpHeaders();
headers.set(CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
Map<String, Object> map = new HashMap<String, Object>();
map.put("messages", mapHeaderMessage);
return new ResponseEntity(map, headers, status);
}
/** /**
* get JSON response from HTTP status only * get JSON response from HTTP status only
* *
@ -97,10 +112,7 @@ public class RestUtil {
} }
public static <T> ResponseEntity<T> getJsonHttptatus(HttpStatus status, Map<String, String> mapHeaderMessage) {
public static <T> ResponseEntity<T> getJsonHttptatus(HttpStatus status,
Map<String, String> mapHeaderMessage) {
HttpHeaders headers = new HttpHeaders(); HttpHeaders headers = new HttpHeaders();
@ -126,7 +138,6 @@ public class RestUtil {
public static <T> ResponseEntity<Set<T>> defaultJsonResponse(Set<T> src) { public static <T> ResponseEntity<Set<T>> defaultJsonResponse(Set<T> src) {
HttpHeaders headers = new HttpHeaders(); HttpHeaders headers = new HttpHeaders();
// headers.add(ACCESS_CONTROL_ALLOW_ORIGIN, "*");
headers.set(CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE); headers.set(CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
return new ResponseEntity<Set<T>>(src, headers, HttpStatus.OK); return new ResponseEntity<Set<T>>(src, headers, HttpStatus.OK);
} }
@ -148,7 +159,7 @@ public class RestUtil {
for (String key : mapHeaderMessage.keySet()) { for (String key : mapHeaderMessage.keySet()) {
headers.add(key, mapHeaderMessage.get(key)); headers.add(key, mapHeaderMessage.get(key));
} }
}// headers.add(ACCESS_CONTROL_ALLOW_ORIGIN, "*"); }
headers.set(CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE); headers.set(CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
return new ResponseEntity<Set<T>>(src, headers, HttpStatus.OK); return new ResponseEntity<Set<T>>(src, headers, HttpStatus.OK);
} }
@ -171,12 +182,10 @@ public class RestUtil {
} }
} }
// headers.add(ACCESS_CONTROL_ALLOW_ORIGIN, "*");
headers.set(CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE); headers.set(CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
return new ResponseEntity<List<T>>(src, headers, HttpStatus.OK); return new ResponseEntity<List<T>>(src, headers, HttpStatus.OK);
} }
/** /**
* get JSON response from List Object * get JSON response from List Object
* *
@ -188,7 +197,6 @@ public class RestUtil {
public static <T> ResponseEntity<List<T>> defaultJsonResponse(List<T> src) { public static <T> ResponseEntity<List<T>> defaultJsonResponse(List<T> src) {
HttpHeaders headers = new HttpHeaders(); HttpHeaders headers = new HttpHeaders();
// headers.add(ACCESS_CONTROL_ALLOW_ORIGIN, "*");
headers.set(CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE); headers.set(CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
return new ResponseEntity<List<T>>(src, headers, HttpStatus.OK); return new ResponseEntity<List<T>>(src, headers, HttpStatus.OK);
} }
@ -204,10 +212,8 @@ public class RestUtil {
Gson gson = new Gson(); Gson gson = new Gson();
HttpHeaders headers = new HttpHeaders(); HttpHeaders headers = new HttpHeaders();
// headers.add(ACCESS_CONTROL_ALLOW_ORIGIN, "*");
headers.set(CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE); headers.set(CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
return new ResponseEntity<String>(gson.toJson(src), headers, return new ResponseEntity<String>(gson.toJson(src), headers, HttpStatus.OK);
HttpStatus.OK);
} }
/** /**