简体   繁体   English

如何在Spring Controller中将JSON转换为Java对象,反之亦然?

[英]How to convert a JSON to java object and vice versa in spring controller?

I want to create a simple Spring project that will serve as a RESTful service. 我想创建一个简单的Spring项目,该项目将用作RESTful服务。

I want to send JSON from frontend and want to convert it to a Java object using @RequestBody . 我想从前端发送JSON,并想使用@RequestBody将其转换为Java对象。 After modifying the object in the backend, I need to convert that object back to JSON and send to front end. 在后端修改对象后,我需要将该对象转换回JSON并发送到前端。

How can I achieve this? 我该如何实现?

You can use the Jackson library. 您可以使用Jackson库。 An example can be found here: http://www.mkyong.com/spring-mvc/spring-3-mvc-and-json-example/ 可以在这里找到一个示例: http : //www.mkyong.com/spring-mvc/spring-3-mvc-and-json-example/

Serialization (POJO -> JSON) and deserialization (JSON -> POJO) in Spring is simply obtained via @RequestBody and @ResponseBody annotations. 只需通过@RequestBody@ResponseBody批注即可获得Spring中的序列化(POJO-> JSON)和反序列化( @ResponseBody > POJO)。

You just need to define a Java class that represents/maps your JSON object on server-side. 您只需要定义一个Java类,即可在服务器端表示/映射您的JSON对象。

Example: 例:

Input JSON 输入JSON

{id: 123, name: "your name", description: ""}

Java class Java类

public class MyClass {
    private int id;
    private String name;
    private String description;
}

Methods in your controller 控制器中的方法

public void postJson(@RequestBody MyClass o){
    // do something...
}

public @ResponseBody MyClass getJson(){
    // do something...
}

NOTE I omitted @RequestMapping settings. 注意我省略了@RequestMapping设置。

You will have to provide csrf token for POST request. 您将必须为POST请求提供csrf令牌。 Instead you can try this. 相反,您可以尝试此。

sending HashMap by angularjs $http.get in spring mvc 在Spring MVC中通过angularjs $ http.get发送HashMap

It works fine just a bit extra @RequestParams but on the better side you can send additional information too and not only the respective object. 它只需要额外的@RequestParams就可以了,但是更好的是,您不仅可以发送相应的对象,还可以发送其他信息。

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

相关问题 将Java Object转换为Json,反之亦然? - Convert Java Object to Json and Vice versa? 在android'java'中如何将Bitmap对象转换为Image对象,反之亦然 - How convert Bitmap Object to Image Object and vice versa in android 'java' 无法将Zookeeper对象转换为json,反之亦然 - cannot convert a zookeeper object to json and vice versa 如何将 POJO 转换为 JSON,反之亦然? - How to convert POJO to JSON and vice versa? 如何在Java中将SID转换为String,反之亦然? - How to convert the SID to String and vice versa in Java? 如何转换ArrayList <object> 转换为字符串,反之亦然 - How to convert ArrayList<object> to String and vice versa 如何在Java中将POJO转换为Map,反之亦然? - How to convert POJO to Map and vice versa in Java? 如何将Java Date转换为OADate,反之亦然? - How to convert Java Date to OADate or vice versa? 将带有嵌入对象的 Java 对象转换为带有属性列表和值列表的 JSON,反之亦然 - Convert Java object with embedded objects to a JSON with list of attributes and list of values and vice versa 如何编写 Apache 骆驼路线来检查 XML 内容并将其转换为 JSON 和 Z3418008FDD81C28 引导服务反之亦然 - How to write a Apache Camel route to check and convert XML contents to JSON and vice-versa for a Spring boot service
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM