clear jackson method
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
package com.imyeyu.api.modules.common.controller;
|
package com.imyeyu.api.modules.common.controller;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||||
import com.fasterxml.jackson.core.type.TypeReference;
|
import com.fasterxml.jackson.core.type.TypeReference;
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
import com.imyeyu.api.bean.CaptchaFrom;
|
import com.imyeyu.api.bean.CaptchaFrom;
|
||||||
@@ -47,8 +48,8 @@ import lombok.Cleanup;
|
|||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.apache.tika.Tika;
|
import org.apache.tika.Tika;
|
||||||
import org.springframework.http.MediaType;
|
|
||||||
import org.springframework.data.mongodb.gridfs.GridFsResource;
|
import org.springframework.data.mongodb.gridfs.GridFsResource;
|
||||||
|
import org.springframework.http.MediaType;
|
||||||
import org.springframework.validation.annotation.Validated;
|
import org.springframework.validation.annotation.Validated;
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
import org.springframework.web.bind.annotation.PathVariable;
|
import org.springframework.web.bind.annotation.PathVariable;
|
||||||
@@ -101,22 +102,6 @@ public class CommonController {
|
|||||||
private final CaptchaManager captchaManager;
|
private final CaptchaManager captchaManager;
|
||||||
private final ResourceHandler resourceHandler;
|
private final ResourceHandler resourceHandler;
|
||||||
|
|
||||||
private String writeJson(Object value) {
|
|
||||||
try {
|
|
||||||
return jackson.writeValueAsString(value);
|
|
||||||
} catch (IOException e) {
|
|
||||||
throw new TimiException(TimiCode.ERROR, "write json error", e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private Map<String, Object> readJsonMap(String value) {
|
|
||||||
try {
|
|
||||||
return jackson.readValue(value, new TypeReference<>() {});
|
|
||||||
} catch (IOException e) {
|
|
||||||
throw new TimiException(TimiCode.ERROR, "read json error", e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@AOPLog
|
@AOPLog
|
||||||
@RequestMapping("")
|
@RequestMapping("")
|
||||||
public String root() {
|
public String root() {
|
||||||
@@ -256,7 +241,7 @@ public class CommonController {
|
|||||||
|
|
||||||
@RequestRateLimit
|
@RequestRateLimit
|
||||||
@GetMapping("/setting/{key}")
|
@GetMapping("/setting/{key}")
|
||||||
public String settingByKey(@PathVariable("key") String key, @RequestParam(value = "as", required = false) Setting.Type asType) {
|
public String settingByKey(@PathVariable("key") String key, @RequestParam(value = "as", required = false) Setting.Type asType) throws JsonProcessingException {
|
||||||
Setting setting = settingService.getByKey(SettingKey.valueOf(key.toUpperCase()));
|
Setting setting = settingService.getByKey(SettingKey.valueOf(key.toUpperCase()));
|
||||||
if (setting.isPrivate()) {
|
if (setting.isPrivate()) {
|
||||||
throw new TimiException(TimiCode.PERMISSION_ERROR);
|
throw new TimiException(TimiCode.PERMISSION_ERROR);
|
||||||
@@ -269,12 +254,12 @@ public class CommonController {
|
|||||||
case JSON -> {
|
case JSON -> {
|
||||||
if (setting.getType() == Setting.Type.YAML) {
|
if (setting.getType() == Setting.Type.YAML) {
|
||||||
Map<String, Object> obj = yaml.load(setting.getValue());
|
Map<String, Object> obj = yaml.load(setting.getValue());
|
||||||
result = writeJson(obj);
|
result = jackson.writeValueAsString(obj);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
case YAML -> {
|
case YAML -> {
|
||||||
if (setting.getType() == Setting.Type.JSON) {
|
if (setting.getType() == Setting.Type.JSON) {
|
||||||
Map<String, Object> obj = readJsonMap(setting.getValue());
|
Map<String, Object> obj = jackson.readValue(setting.getValue(), new TypeReference<>() {});
|
||||||
result = yaml.dump(obj);
|
result = yaml.dump(obj);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -284,7 +269,7 @@ public class CommonController {
|
|||||||
|
|
||||||
@RequestRateLimit
|
@RequestRateLimit
|
||||||
@PostMapping("/setting/map")
|
@PostMapping("/setting/map")
|
||||||
public Map<SettingKey, String> mapSettingByKeys(@RequestBody Map<SettingKey, Map<String, Object>> settingMap) {
|
public Map<SettingKey, String> mapSettingByKeys(@RequestBody Map<SettingKey, Map<String, Object>> settingMap) throws JsonProcessingException {
|
||||||
List<Setting> result = settingService.listByKeys(new ArrayList<>(settingMap.keySet()));
|
List<Setting> result = settingService.listByKeys(new ArrayList<>(settingMap.keySet()));
|
||||||
for (int i = 0; i < result.size(); i++) {
|
for (int i = 0; i < result.size(); i++) {
|
||||||
Setting setting = result.get(i);
|
Setting setting = result.get(i);
|
||||||
@@ -300,12 +285,12 @@ public class CommonController {
|
|||||||
case JSON -> {
|
case JSON -> {
|
||||||
if (setting.getType() == Setting.Type.YAML) {
|
if (setting.getType() == Setting.Type.YAML) {
|
||||||
Map<String, Object> obj = new Yaml().load(setting.getValue());
|
Map<String, Object> obj = new Yaml().load(setting.getValue());
|
||||||
setting.setValue(writeJson(obj));
|
setting.setValue(jackson.writeValueAsString(obj));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
case YAML -> {
|
case YAML -> {
|
||||||
if (setting.getType() == Setting.Type.JSON) {
|
if (setting.getType() == Setting.Type.JSON) {
|
||||||
Map<String, Object> obj = readJsonMap(setting.getValue());
|
Map<String, Object> obj = jackson.readValue(setting.getValue(), new TypeReference<>() {});
|
||||||
setting.setValue(new Yaml().dump(obj));
|
setting.setValue(new Yaml().dump(obj));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user