openfeign远程调用时JSON解析错误

1.json Cannot deserialize instance of `java.util.ArrayList<com.uj.toolmanager.parambase.entity.SysParam>` out of START_OBJECT token

表明远程调用返回的是一个object对象,而客户端却用了一个列表来接收。

2.Exception in thread "main" com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `com.uj.toolmanager.parambase.entity.SysParam` out of START_ARRAY token

表明远程调用返回的是一个数组对象,而客户端却用了一个对象来接收。

3.要正常解析json字符串或者数据要使用专门的工具类,

objectMapper解析数组

String json3="[\n" +
                "    {\n" +
                "  \"workshop\": 483,\n" +
                "  \"productLine\": 484,\n" +
                "  \"orgId\": 1,\n" +
                "  \"itemCode\": \"helperName\",\n" +
                "  \"itemSubname\": \"辅料1\",\n" +
                "  \"itemSort\": 0,\n" +
                "  \"itemUserCode\": \"1111\",\n" +
                "  \"isGobal\": 1,\n" +
                "  \"remark\": \"1\"\n" +
                "},\n" +
                "{\n" +
                "  \"workshop\": 483,\n" +
                "  \"productLine\": 484,\n" +
                "  \"orgId\": 1,\n" +
                "  \"itemCode\": \"helperName\",\n" +
                "  \"itemSubname\": \"辅料1\",\n" +
                "  \"itemSort\": 0,\n" +
                "  \"itemUserCode\": \"1111\",\n" +
                "  \"isGobal\": 1,\n" +
                "  \"remark\": \"1\"\n" +
                "}]";
ObjectMapper objectMapper = new ObjectMapper();
//        Map<String, Object> stringList = objectMapper.readValue(json3, new TypeReference<Map<String,Object>>() {});
List<SysParam> stringList = objectMapper.readValue(json3, new TypeReference<List<SysParam>>() {});
//        Integer o = (Integer) stringList.get("code");
//        SysParam sysParam = stringList.get(0);
System.out.println(stringList);

objectMapper解析字符串

 public static void main(String[] args) throws JsonProcessingException {
        String json2="{\"code\":500,\"data\":{\"addressArea\":1,\"addressCity\":1,\"addressDetail\":\"123\",\"adressProvince\":1,\"contractPhone\":\"18866665555\",\"contractUser\":\"e\",\"createTime\":1566806029000,\"createUserId\":-1,\"eMail\":\"12@qq.com\",\"isDeleted\":0,\"mdKey\":null,\"netAddress\":\"xxx\",\"orgChannel\":\"org_channel\",\"orgId\":1,\"orgLogoUrl\":\"/images/20240628/966484267766906880.png\",\"orgName\":\"1\",\"orgShortName\":\"1\",\"orgType\":1,\"phone\":\"0571-xxxxxxxx\",\"smallLogoUrl\":\"aliyuncs.\",\"status\":0,\"updateTime\":1661526859000,\"updateUserId\":1},\"resultMsg\":\"加载完成\"}";

        ObjectMapper objectMapper = new ObjectMapper();
        Map<String, Object> stringList = objectMapper.readValue(json2, new TypeReference<Map<String,Object>>() {});
        Integer o = (Integer) stringList.get("code");
        System.out.println(o);

    }

当然,我可以为您提供OpenFeign远程调用的示例。首先,请确保您已经在项目中引入了OpenFeign的依赖。 接下来,您需要定义一个Feign客户端接口,用于声明远程调用的方法。这里以调用一个名为"example-service"的远程服务为例,示例代码如下: ```java import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.GetMapping; @FeignClient(name = "example-service") public interface ExampleServiceClient { @GetMapping("/api/example") String getExampleData(); } ``` 在上述示例中,我们使用`@FeignClient`注解指定了远程服务的名称,这里是"example-service"。然后通过`@GetMapping`注解指定了远程调用的URL路径。 接下来,您需要在启动类上添加`@EnableFeignClients`注解,以启用Feign客户端的自动配置。 完成以上准备工作后,您就可以在需要调用远程服务的地方注入`ExampleServiceClient`接口,并使用其中定义的方法进行远程调用了。示例代码如下: ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class ExampleController { @Autowired private ExampleServiceClient exampleServiceClient; @GetMapping("/example") public String getExampleData() { return exampleServiceClient.getExampleData(); } } ``` 在上述示例中,我们通过`@Autowired`注解将`ExampleServiceClient`接口注入到`ExampleController`中,并在`getExampleData()`方法中调用该接口的方法。 这就是一个简单的OpenFeign远程调用的示例。您可以根据实际需要,按照类似的方式进行远程调用。希望对您有帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值