【原创】SpringBoot入门学习(2)编写第一个正式的接口

5 篇文章 0 订阅
3 篇文章 0 订阅

我们都知道一个spring boot的主要作用就是为了接口的实现,接口的实现当中少不了对于Jeson的运用

接下来就是我们第一个关于Jeson接口的实现(其中模型、协议相关内容就不展示了)

package com.example.demo;

import com.google.gson.Gson;  //关于Gsonde的包引用
import model.Human;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/BaseBusiness")
public class BaseBusiness {
    @RequestMapping("/Index")
    public String Index() {
        return "Hello Business";
    }

    @RequestMapping("/ShowEnterJeson")
    public  String ShowEnterJeson(@RequestBody String str){
        //声明Gson对象
        Gson gson = new Gson();
        //利用方法fromJson对Json进行转换()
        String reslt = gson.fromJson(str, String.class);
        return str+"<br>"+reslt;
    }

    @RequestMapping("/HumanToJeson")
    public  String HumanToJeson(@RequestBody String str){
        String result="";
        Gson gson = new Gson();
        //对Json进行转换(转换成模型对象)
        Human human=gson.fromJson(str, Human.class);
        result+=("姓名为:"+ human.name+"\n");
        result+=("年龄为:"+ human.age+"\n");
        result+=("性别为:"+ human.sex+"\n");
        return result;
    }

}

对于喜欢使用Maven的程序可以使用下面的配置载入Gson库

pom.xml文件配置(放到<dependencies>里面后up项目即可)

    <dependency>
        <groupId>com.google.code.gson</groupId>
        <artifactId>gson</artifactId>
        <version>2.2.4</version>
    </dependency>

更多关于Gson的用可以访问下面地址

Gson下载:https://blog.csdn.net/qq_29503199/article/details/91420109
Java当中Gson的使用:https://blog.csdn.net/qq_29503199/article/details/90713025

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值