简体   繁体   English

如何将QueryParams中的JSON转换为Java中的Object Spring?

[英]How to convert JSON in QueryParams into Object in Java Spring?

I have this query我有这个查询

http://localhost:8555/list/csv?search={}

Where search is a json object (omitted other params as they are irrelevant here).其中搜索是 json object(省略其他参数,因为它们与此处无关)。 How can i convert this into a nested object?我如何将其转换为嵌套的 object?

public record CsvParams<T>(
    T search,
    /* Other query params */ ) {}

Right now im getting error that string cannot be cast into object.现在我收到错误消息,无法将字符串转换为 object。

class java.lang.String cannot be cast to class classname

Is there anyway to do this?有没有办法做到这一点? Old solution uses ObjectMapper to convert string into corresbonding object. I was hoping that maybe there is a way to do it more simpli and remove this boilerplate.旧的解决方案使用 ObjectMapper 将字符串转换为对应的 object。我希望也许有一种方法可以更简单地完成它并删除这个样板。

Any single value of a query param can't be automatically converted to a non-primitive type.查询参数的任何单个值都不能自动转换为非原始类型。 You can convert multiple params to a class, but not one that happens to be a JSON AFAIK.您可以将多个参数转换为 class,但不能转换为 JSON AFAIK。 But you can create a converter custom deserialiser and then use it in different controllers, but in the end you'd still use an ObjectMapper .但是您可以创建一个转换器自定义反序列化器,然后在不同的控制器中使用它,但最后您仍然会使用ObjectMapper

More info on how to do the latter here: https://www.baeldung.com/spring-mvc-send-json-parameters有关如何在此处执行后者的更多信息: https://www.baeldung.com/spring-mvc-send-json-parameters

If you have to work with query params than I don't think you can have it converted automatically by Spring boot.如果您必须使用查询参数,我认为您不能通过 Spring 引导自动转换它。 But if you work with POST or PUT methods and can pass your params as request params in request body your JSON params can be automatically converted to class instances by Spring boot and no effort required from you.但是,如果您使用 POST 或 PUT 方法,并且可以将您的参数作为请求参数传递到请求正文中,您的 JSON 参数可以通过 Spring 引导自动转换为 class 实例,您无需任何努力。 However, if you have to work with query param (say you have to use method GET so you have no request body) than you can use Json-Jackson library or Gson library to parse your Json into class instance.但是,如果您必须使用查询参数(假设您必须使用 GET 方法,因此您没有请求主体),则可以使用 Json-Jackson 库或 Gson 库将 Json 解析为 class 实例。 If you use Jackson you will need to use class ObjectMapper.如果您使用 Jackson,则需要使用 class ObjectMapper。 For Jackson lib info see this site , for ObjectMapper class see Javadoc here .有关 Jackson 库信息,请参阅此站点,有关ObjectMapper class,请参阅此处的 Javadoc However, I wrote my own JsonUtils that is very good for simple usecases like yours.但是,我编写了自己的 JsonUtils,它非常适合像您这样的简单用例。 It allows to to parse simple JSON into a class with a single method.它允许使用单一方法将简单的 JSON 解析为 class。 It is very simple and strait forward.这是非常简单和直截了当的。 It is a thin wrapper over Jackson library.它是 Jackson 库的精简包装器。 See the Javadoc for method readObjectFromJsonString .请参阅 Javadoc 以了解方法readObjectFromJsonString Class JsonUtils is part of Open Source MgntUtils library. Class JsonUtils 是开源 MgntUtils 库的一部分。 You can get it as Maven artifact on Maven Central and as a jar (with source code and Javadoc) on Github您可以在Maven Central上以 Maven 工件的形式获得它,在 Github 上以jar (带有源代码和 Javadoc)的形式获得它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM