runliuv

runliuv@cnblogs

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

1. 在https://start.spring.io/ 网站初始化一个springboot 项目(2.XX.XX 版本),下载后本地解压。

如果阿里云仓库没这版本,手动修改POM ,version改为2.4.0。

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.0</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

 

 

2.先在pom.xml添加 json 库:

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

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

3.修改application.properties,增加一行:

server.port=12777

指定运行端口。 

 

4 增加

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;
}    


}

--

5. 在 xxxxApplication 类中,shift+F9 ,DEBUG运行

 

post main 测试:

 

--

posted on 2019-05-22 22:09  runliuv  阅读(338)  评论(0编辑  收藏  举报