From 8279e99206e8cb3daaa557f746673ee99a631b1f Mon Sep 17 00:00:00 2001 From: qinyingjie Date: Mon, 9 Jan 2023 14:13:09 +0800 Subject: [PATCH] =?UTF-8?q?fix:=E5=BE=AE=E6=9C=8D=E5=8A=A1=E4=BB=A3?= =?UTF-8?q?=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- springcloud-service-common/pom.xml | 7 +++++++ .../com/kwan/springcloud/response/Result.java | 2 +- springcloud-service-eureka/pom.xml | 5 +++++ springcloud-service-goods/pom.xml | 14 ++++++-------- .../controller/HelloController.java | 19 +++++++++++-------- .../controller/KwanGoodsInfoController.java | 10 ++-------- 6 files changed, 32 insertions(+), 25 deletions(-) diff --git a/springcloud-service-common/pom.xml b/springcloud-service-common/pom.xml index 4eed6cf..49f398a 100644 --- a/springcloud-service-common/pom.xml +++ b/springcloud-service-common/pom.xml @@ -35,6 +35,13 @@ true + + + com.alibaba + fastjson + 1.2.83 + + \ No newline at end of file diff --git a/springcloud-service-common/src/main/java/com/kwan/springcloud/response/Result.java b/springcloud-service-common/src/main/java/com/kwan/springcloud/response/Result.java index 37b23ad..ab10397 100644 --- a/springcloud-service-common/src/main/java/com/kwan/springcloud/response/Result.java +++ b/springcloud-service-common/src/main/java/com/kwan/springcloud/response/Result.java @@ -76,7 +76,7 @@ public class Result implements Serializable { } public static Result ok(String msg) { - Result r = new Result(); + Result r = new Result<>(); r.setSuccess(true); r.setCode(CommonConstant.SC_OK_200); r.setMessage(msg); diff --git a/springcloud-service-eureka/pom.xml b/springcloud-service-eureka/pom.xml index 86844d7..0666cba 100644 --- a/springcloud-service-eureka/pom.xml +++ b/springcloud-service-eureka/pom.xml @@ -34,6 +34,11 @@ + + org.springframework.boot + spring-boot-starter-actuator + + diff --git a/springcloud-service-goods/pom.xml b/springcloud-service-goods/pom.xml index b2396b6..43a7b29 100644 --- a/springcloud-service-goods/pom.xml +++ b/springcloud-service-goods/pom.xml @@ -23,6 +23,12 @@ 1.0.0 compile + + + org.springframework.boot + spring-boot-starter-web + + org.springframework.boot spring-boot-starter-test @@ -57,14 +63,6 @@ org.springframework.cloud spring-cloud-starter-netflix-eureka-server - - - com.google.code.gson - gson - 2.9.0 - - - diff --git a/springcloud-service-goods/src/main/java/com/kwan/springcloud/controller/HelloController.java b/springcloud-service-goods/src/main/java/com/kwan/springcloud/controller/HelloController.java index e4c225c..71ebdda 100644 --- a/springcloud-service-goods/src/main/java/com/kwan/springcloud/controller/HelloController.java +++ b/springcloud-service-goods/src/main/java/com/kwan/springcloud/controller/HelloController.java @@ -1,12 +1,14 @@ package com.kwan.springcloud.controller; -import com.google.gson.Gson; +import com.alibaba.fastjson.JSONArray; +import com.kwan.springcloud.response.Result; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cloud.client.ServiceInstance; import org.springframework.cloud.client.discovery.DiscoveryClient; import org.springframework.cloud.client.serviceregistry.Registration; +import org.springframework.http.MediaType; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @@ -22,14 +24,15 @@ public class HelloController { private Registration registration; - @RequestMapping("/hello") - public String index() { - + /** + * produces = MediaType.APPLICATION_PROBLEM_JSON_VALUE + * 解决不是json显示问题 + */ + @RequestMapping(value = "/hello", produces = MediaType.APPLICATION_PROBLEM_JSON_VALUE) + public Result index() { List instances = client.getInstances(registration.getServiceId()); - - log.info(new Gson().toJson(instances)); - - return "hello world"; + log.info(JSONArray.toJSONString(instances)); + return Result.ok(instances); } } diff --git a/springcloud-service-goods/src/main/java/com/kwan/springcloud/controller/KwanGoodsInfoController.java b/springcloud-service-goods/src/main/java/com/kwan/springcloud/controller/KwanGoodsInfoController.java index f47a0c9..35e64f9 100644 --- a/springcloud-service-goods/src/main/java/com/kwan/springcloud/controller/KwanGoodsInfoController.java +++ b/springcloud-service-goods/src/main/java/com/kwan/springcloud/controller/KwanGoodsInfoController.java @@ -1,23 +1,17 @@ package com.kwan.springcloud.controller; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; - import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.kwan.springcloud.entity.KwanGoodsInfo; import com.kwan.springcloud.response.Result; import com.kwan.springcloud.service.KwanGoodsInfoService; +import org.springframework.http.MediaType; import org.springframework.web.bind.annotation.*; import javax.annotation.Resource; import java.io.Serializable; import java.util.List; -/** - * (KwanGoodsInfo)表控制层 - * - * @author makejava - * @since 2023-01-08 10:21:26 - */ @RestController @RequestMapping("kwanGoodsInfo") public class KwanGoodsInfoController { @@ -45,7 +39,7 @@ public class KwanGoodsInfoController { * @param id 主键 * @return 单条数据 */ - @GetMapping("{id}") + @GetMapping(value = "{id}", produces = MediaType.APPLICATION_PROBLEM_JSON_VALUE) public Result selectOne(@PathVariable Serializable id) { return Result.ok(this.kwanGoodsInfoService.getById(id)); } -- GitLab