diff --git a/smtweb-framework/sw-framework-core/src/main/java/cc/smtweb/framework/core/mvc/config/WebMvcConfig.java b/smtweb-framework/sw-framework-core/src/main/java/cc/smtweb/framework/core/mvc/config/WebMvcConfig.java index c1b8101..aa35f29 100644 --- a/smtweb-framework/sw-framework-core/src/main/java/cc/smtweb/framework/core/mvc/config/WebMvcConfig.java +++ b/smtweb-framework/sw-framework-core/src/main/java/cc/smtweb/framework/core/mvc/config/WebMvcConfig.java @@ -16,8 +16,10 @@ import org.springframework.boot.autoconfigure.http.HttpMessageConverters; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; +import org.springframework.http.MediaType; import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; import org.springframework.web.method.support.HandlerMethodArgumentResolver; +import org.springframework.web.servlet.config.annotation.ContentNegotiationConfigurer; import org.springframework.web.servlet.config.annotation.CorsRegistry; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; @@ -121,4 +123,10 @@ public class WebMvcConfig implements WebMvcConfigurer { log.error("'/files/**' 已经被注册"); } } + + // 所有返回接口Content-Type默认都返回application/json + @Override + public void configureContentNegotiation(ContentNegotiationConfigurer configurer) { + configurer.defaultContentType(MediaType.APPLICATION_JSON); + } } diff --git a/smtweb-framework/sw-framework-core/src/main/java/cc/smtweb/framework/core/mvc/controller/ApiController.java b/smtweb-framework/sw-framework-core/src/main/java/cc/smtweb/framework/core/mvc/controller/ApiController.java index 6b4917c..4ee218c 100644 --- a/smtweb-framework/sw-framework-core/src/main/java/cc/smtweb/framework/core/mvc/controller/ApiController.java +++ b/smtweb-framework/sw-framework-core/src/main/java/cc/smtweb/framework/core/mvc/controller/ApiController.java @@ -1,6 +1,7 @@ package cc.smtweb.framework.core.mvc.controller; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.MediaType; import org.springframework.web.bind.annotation.*; import javax.annotation.PreDestroy; @@ -25,7 +26,7 @@ public class ApiController { * @return 返回对象,一般在函数里面使用R * @throws Exception API处理异常 */ - @GetMapping("/api/**") + @GetMapping(value = "/api/**", produces = {MediaType.APPLICATION_JSON_UTF8_VALUE}) public Object commonGet(@RequestParam Map params, HttpServletRequest request, HttpServletResponse response) throws Exception { String apiUrl = request.getRequestURI().substring(5); @@ -41,7 +42,7 @@ public class ApiController { * @return 返回对象,一般在函数里面使用R * @throws Exception API处理异常 */ - @PostMapping("/api/**") + @PostMapping(value = "/api/**", produces = {MediaType.APPLICATION_JSON_UTF8_VALUE}) public Object commonPost(@RequestParam Map params, @RequestBody(required = false) String body, HttpServletRequest request, HttpServletResponse response) throws Exception { String apiUrl = request.getRequestURI().substring(5); diff --git a/smtweb-framework/sw-framework-core/src/main/java/cc/smtweb/framework/core/util/JsonUtil.java b/smtweb-framework/sw-framework-core/src/main/java/cc/smtweb/framework/core/util/JsonUtil.java index 169be36..f752dd9 100644 --- a/smtweb-framework/sw-framework-core/src/main/java/cc/smtweb/framework/core/util/JsonUtil.java +++ b/smtweb-framework/sw-framework-core/src/main/java/cc/smtweb/framework/core/util/JsonUtil.java @@ -7,6 +7,7 @@ import cc.smtweb.framework.core.db.vo.ModelTable; import cc.smtweb.framework.core.exception.JsonParseException; import cc.smtweb.framework.core.util.jackson.*; import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.JavaType; @@ -69,6 +70,7 @@ public class JsonUtil { module.addDeserializer(java.util.Date.class, new DateDeserializer()); mapper.registerModule(module); + mapper.enable(JsonGenerator.Feature.WRITE_BIGDECIMAL_AS_PLAIN); } public static JsonNode readTree(String body) { diff --git a/smtweb-framework/sw-framework-core/src/main/resources/config/demo.json b/smtweb-framework/sw-framework-core/src/main/resources/config/demo.json index e69de29..0631cf2 100644 --- a/smtweb-framework/sw-framework-core/src/main/resources/config/demo.json +++ b/smtweb-framework/sw-framework-core/src/main/resources/config/demo.json @@ -0,0 +1,111 @@ +{ + "fields": [ + { + "name": "mc_id", + "fieldType": "1", + "dataType": "ID", + "null": "1", + "default": "-1", + "title": "ID" + }, + { + "name": "mc_parent_id", + "fieldType": "4", + "dataType": "ID", + "null": "1", + "default": "-1", + "title": "ID", + "link": "2" + }, + { + "name": "mc_prj_id", + "fieldType": "", + "dataType": "ID", + "null": "1", + "default": "-1", + "title": "ID", + "link": "1" + }, + { + "name": "mc_code", + "fieldType": "2", + "dataType": "CODE", + "null": "0", + "default": "", + "title": "编码", + "link": "", + "editor": "" + }, + { + "name": "mc_name", + "fieldType": "3", + "dataType": "NAME", + "null": "0", + "default": "", + "title": "编码", + "link": "", + "editor": "" + }, + { + "name": "mc_create_uid", + "fieldType": "7", + "dataType": "ID", + "null": "1", + "default": "-1", + "title": "创建人", + "link": "", + "editor": "" + }, + { + "name": "mc_update_uid", + "fieldType": "9", + "dataType": "ID", + "null": "1", + "default": "-1", + "title": "最后更新人", + "link": "", + "editor": "" + }, + { + "name": "mc_create_at", + "fieldType": "8", + "dataType": "DATETIME", + "null": "1", + "default": "-1", + "title": "创建时间", + "link": "", + "editor": "" + }, + { + "name": "mc_update_at", + "fieldType": "10", + "dataType": "DATETIME", + "null": "1", + "default": "-1", + "title": "最后更新时间", + "link": "", + "editor": "" + } + ], + "indexes": [ + { + "name": "pk", + "fields": "mc_id", + "type": "P" + } + ], + "caches": [ + { + "name": "pr", + "title": "按树型", + "fields": "mc_parent_id", + "type": "L" + }, + { + "name": "prj", + "title": "按目录", + "fields": "mc_prj_id", + "type": "L" + } + ] +} \ No newline at end of file diff --git a/smtweb-framework/sw-framework-core/target/classes/config/demo.json b/smtweb-framework/sw-framework-core/target/classes/config/demo.json index e69de29..0631cf2 100644 --- a/smtweb-framework/sw-framework-core/target/classes/config/demo.json +++ b/smtweb-framework/sw-framework-core/target/classes/config/demo.json @@ -0,0 +1,111 @@ +{ + "fields": [ + { + "name": "mc_id", + "fieldType": "1", + "dataType": "ID", + "null": "1", + "default": "-1", + "title": "ID" + }, + { + "name": "mc_parent_id", + "fieldType": "4", + "dataType": "ID", + "null": "1", + "default": "-1", + "title": "ID", + "link": "2" + }, + { + "name": "mc_prj_id", + "fieldType": "", + "dataType": "ID", + "null": "1", + "default": "-1", + "title": "ID", + "link": "1" + }, + { + "name": "mc_code", + "fieldType": "2", + "dataType": "CODE", + "null": "0", + "default": "", + "title": "编码", + "link": "", + "editor": "" + }, + { + "name": "mc_name", + "fieldType": "3", + "dataType": "NAME", + "null": "0", + "default": "", + "title": "编码", + "link": "", + "editor": "" + }, + { + "name": "mc_create_uid", + "fieldType": "7", + "dataType": "ID", + "null": "1", + "default": "-1", + "title": "创建人", + "link": "", + "editor": "" + }, + { + "name": "mc_update_uid", + "fieldType": "9", + "dataType": "ID", + "null": "1", + "default": "-1", + "title": "最后更新人", + "link": "", + "editor": "" + }, + { + "name": "mc_create_at", + "fieldType": "8", + "dataType": "DATETIME", + "null": "1", + "default": "-1", + "title": "创建时间", + "link": "", + "editor": "" + }, + { + "name": "mc_update_at", + "fieldType": "10", + "dataType": "DATETIME", + "null": "1", + "default": "-1", + "title": "最后更新时间", + "link": "", + "editor": "" + } + ], + "indexes": [ + { + "name": "pk", + "fields": "mc_id", + "type": "P" + } + ], + "caches": [ + { + "name": "pr", + "title": "按树型", + "fields": "mc_parent_id", + "type": "L" + }, + { + "name": "prj", + "title": "按目录", + "fields": "mc_prj_id", + "type": "L" + } + ] +} \ No newline at end of file