springboot maven 收发JSON

先在pom.xml添加 json 库

<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.35</version>
</dependency>

controller :

package com.example.demo.controller;

import com.example.demo.RequestCon;
import com.example.demo.ReturnBody;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HomeController {

    @RequestMapping("/test")
    public String Index() {

        return "1111";
    }

    @RequestMapping("/test2")
    public ReturnBody test2(@RequestBody RequestCon req) {

        ReturnBody rb = new ReturnBody();

        try {
            rb.code = "server 0";
            rb.setMsg(req.msg + "  server");

        } catch (Exception ex) {

            rb.setCode("0");
            rb.setMsg("ex " + ex.getMessage());

        }
        return rb;
    }

}
RequestCon:
package com.example.demo;

public class RequestCon {

    public String code;
    public String msg;


/**
 * @param code the code to set
 */
public void setCode(String code) {
    this.code = code;
}
/**
 * @return the code
 */
public String getCode() {
    return code;
}
/**
 * @param msg the msg to set
 */
public void setMsg(String msg) {
    this.msg = msg;
}
/**
 * @return the msg
 */
public String getMsg() {
    return msg;
}

}
ReturnBody:
package com.example.demo;

public class ReturnBody {

    public String code;
    public String msg;


/**
 * @param code the code to set
 */
public void setCode(String code) {
    this.code = code;
}
/**
 * @return the code
 */
public String getCode() {
    return code;
}
/**
 * @param msg the msg to set
 */
public void setMsg(String msg) {
    this.msg = msg;
}
/**
 * @return the msg
 */
public String getMsg() {
    return msg;
}    


}

--

post main 测试:

--

原文地址:https://www.cnblogs.com/runliuv/p/10908903.html