json中的Json在Spring Boot中不起作用

迪帕克·辛格(Deepak Singh):

我正在尝试在JSON请求中使用JSON。

例如:

{
"name":"newdeeeepaajlf",
"category":"fsafaa",
"jsonData":{
   "a":"value"
}
}

现在,当我尝试将其放入我的DTO中时,

private JSONObject jsonData;

它被转换为空白JSON

{}

我陷入了困境。

卡雷普:

我们可以用地图来转换数据

public class TestModel {
    private String name;
    private String category;
    private Map<String, Object> jsonObj;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getCategory() {
        return category;
    }

    public void setCategory(String category) {
        this.category = category;
    }

    public Map<String, Object> getJsonObj() {
        return jsonObj;
    }

    public void setJsonObj(Map<String, Object> jsonObj) {
        this.jsonObj = jsonObj;
    }

}

并使用像下面这样的控制器上的类

@PostMapping("/test")
    public boolean test(@RequestBody TestModel model) {

        System.out.println(model.getCategory());
        System.out.println(model.getName());
        JSONObject jsonObj = new JSONObject(model.getJsonObj());
        System.out.println(jsonObj);

        return true;
    }

要求

{
    "category":"json",
    "name":"name",
    "jsonObj": {
        "a": "value"
    }
}

它将打印

json
name
{a=value}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

WebMvcConfigurer 在 Spring Boot 中不起作用?

@Transactional在带有CrudRepository的Spring Boot中不起作用

Spring Boot-@PreAuthorize在测试中不起作用

RestController在oauth2 Spring Boot中不起作用

Spring Boot 2.2.2 - Prometheus 在 Actuator 中不起作用

多模块组件扫描在Spring Boot中不起作用

注释CrossOrigin在Spring Boot中不起作用

我的BeanPostProcessor在Spring Boot中不起作用

Spring Boot - @Autowired 在 @Service 中不起作用

自定义MessageConverter在Spring Boot中不起作用

Spring Boot Kotlin项目在Eclipse中不起作用

Spring Boot DevTools在Eclipse中不起作用

Spring Boot @Scheduler 在 Google Kubernetes Engine 中不起作用

Primefaces FileUpload在Spring Boot中不起作用

spring boot中的BindingResult参数不起作用

实时重载在Spring Boot devtools中不起作用

事务注释在Spring Boot中不起作用

为什么RestController在Spring Boot中不起作用?

Spring Boot permitAll在WebSecurityConfigurerAdapter中不起作用

CommonsRequestLoggingFilter在Spring Boot应用程序中不起作用

@ConstructorBinding 在 Spring Boot 2.5.4 中似乎不起作用

部署的Spring-Boot战争在Tomcat中不起作用

Spring Boot中的异常处理程序不起作用?

Spring Boot方面在我的API中不起作用

全局CORS配置在Spring Boot中不起作用

Spring Boot Autowired在配置类中不起作用

@Service类中的Spring Boot缓存不起作用

事务注释在使用JPA的Spring Boot中不起作用

MultiPart PUT 请求在 spring boot 中不起作用