gpt4 book ai didi

java - Spring MVC、JSON、长度超过 1000 个字符的字符串字段的错误序列化

转载 作者:行者123 更新时间:2023-12-02 04:57:00 24 4
gpt4 key购买 nike

我尝试通过注释 @ResponseBody 将实体列表作为 JSON 数据从服务器发送到客户端。问题是长度超过 1000 的文本字段序列化不正确。在这种情况下,字符串将替换为空符号“\u0000”序列。

我配置 JSON 映射器:

@Configuration
@EnableWebMvc
@EnableScheduling
@ComponentScan({"configs", "controllers", "services", "models"})
public class WebConfig
{
@Bean RequestMappingHandlerAdapter requestMappingHandlerAdapter()
{
RequestMappingHandlerAdapter reqMapHAdapter=new RequestMappingHandlerAdapter();

Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();
builder.indentOutput(true);
MappingJackson2HttpMessageConverter httpMsgConv=new MappingJackson2HttpMessageConverter(builder.build());
httpMsgConv.setPrettyPrint(true);

ArrayList<MediaType> mediaTypes=new ArrayList<>();
mediaTypes.add(new MediaType("application", "json", StandardCharsets.UTF_8));
httpMsgConv.setSupportedMediaTypes(mediaTypes);

ArrayList<HttpMessageConverter<?>> msgConvs=new ArrayList<>();
msgConvs.add(httpMsgConv);

reqMapHAdapter.setMessageConverters(msgConvs);
return reqMapHAdapter;
}
...
}

Controller 的方法:

@Controller
public class MyController
{
@RequestMapping(value={GET_MESSAGES_URL}, method=RequestMethod.GET)
@ResponseBody List<MessageViewItem> getMessages()
{
// getting List<MesssageViewItem>
List<MessageViewItem> messages=...;
return messages;
}
}

MessageViewItem 类:

public class MessageViewItem 
{
private String text,
localizedSendingDate;

private UserShortViewItem author;

private Long messageId;

// default constructor, getters and setters ...
}

结果,如果text.length()<=1000:

[ {
"text" : "123",
"localizedSendingDate" : "1 day ago",
"author" : {
"viewName" : "John Doe",
"imageUrl" : "/test_web_spring/images/usersImages/0/0/4.jpg",
"id" : 940
},
"messageId" : 3540
} ]

如果text.length()>1000(我从消息中删除了大部分“\u0000”):

[ {
"text" : " : \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000
\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000",
"localizedSendingDate" : "3 hour ago",
"author" : {
"viewName" : "John Doe",
"imageUrl" : "/test_web_spring/images/usersImages/0/0/4.jpg",
"id" : 940
},
"messageId" : 3812
} ]

尽管如此,手动调用 ObjectMapper.writeValueAsString 按预期工作,并且通常会序列化长度超过 1000 个字符的文本字段。

有什么想法,如何解决这个问题吗?

添加:测试 Controller 的方法:

@RequestMapping(value={"/testJsonConv"}, method=RequestMethod.GET)
@ResponseBody MessageViewItem testJsonConv()
{
MessageViewItem msgViewItem=new MessageViewItem();
msgViewItem.setText("Sed ut perspiciatis, unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam eaque ipsa, quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt, explicabo. nemo enim ipsam voluptatem, quia voluptas sit, aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos, qui ratione voluptatem sequi nesciunt, neque porro quisquam est, qui dolorem ipsum, quia dolor sit, amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt, ut labore et dolore magnam aliquam quaerat voluptatem. ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? quis autem vel eum iure reprehenderit, qui in ea voluptate velit esse, quam nihil molestiae consequatur, vel illum, qui dolorem eum fugiat, quo voluptas nulla pariatur?Sed ut perspiciatis, unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam eaque ipsa, quae ab illo");
return msgViewItem;
}

结果(剪切大部分“\u0000”):

{
"text" : " : \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000",
"localizedSendingDate" : null,
"author" : null,
"messageId" : null
}

最佳答案

我看到两种可能的解决方案:

  1. 使用显式转换为 JSON,如下所示:

    @ Controller 公共(public)类 MyController{ @Autowired ObjectMapper 映射器;

    // manually convert you'r data to JSON string, and send as text
    @RequestMapping(value={GET_MESSAGES_URL},
    method=RequestMethod.GET, produces=MediaType.TEXT_PLAIN) throws ...
    @ResponseBody String getMessages()
    {
    // getting List<MesssageViewItem>
    List<MessageViewItem> messages=...;
    return mapper.writeValueAsString(messages);
    }

    }

不要忘记添加文本媒体类型的消息转换器:

@Configuration
@EnableScheduling
@EnableAsync
@ComponentScan({"configs", "models"})
public class WebConfig
{
@Bean RequestMappingHandlerAdapter requestMappingHandlerAdapter()
{
RequestMappingHandlerAdapter reqMapHAdapter=new RequestMappingHandlerAdapter();

ArrayList<HttpMessageConverter<?>> msgConvs=new ArrayList<>();

StringHttpMessageConverter stringConverter = new StringHttpMessageConverter(Charset.forName("UTF-8"));
stringConverter.setSupportedMediaTypes(Arrays.asList(MediaType.TEXT_PLAIN));
msgConvs.add(stringConverter);

reqMapHAdapter.setMessageConverters(msgConvs);
return reqMapHAdapter;
}

...
}
  • 更新依赖项后,我的项目问题就解决了。我将 spring 更新到 4.2.4 和(可能是最重要的)- jackson,从 codehaus 更新到最新的 fastxml 版本。
  • 这是一篇帖子,我想将其标记为答案。作者建议更新 jackson 库,它对我有帮助。但,不幸的是,我找不到这篇文章。

    关于java - Spring MVC、JSON、长度超过 1000 个字符的字符串字段的错误序列化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32241577/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com