zoukankan      html  css  js  c++  java
  • 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 测试:

    --

  • 相关阅读:
    shell脚本,通过传入的参数来计算最大值和最小值以及平均值。
    mac date命令
    jstorm系列-2:入门
    jstorm系列-1:入门
    git 如何恢复只是提交到本地的文件(或者commit)
    shell 参数
    shell 运算符
    shell 中的<,<<,>,>>
    shell 学习笔记
    java 多线程剖析
  • 原文地址:https://www.cnblogs.com/runliuv/p/10908903.html
Copyright © 2011-2022 走看看