【SpringBoot】springBoot内JSON --数据交换语言:来信息交互

在这里插入图片描述
springBoot内使用JSON,返回对象

–pom.xml 中添加 Web 依赖:

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


–启动类:

package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class WebApplication {
	public static void main(String[] args) {
		SpringApplication.run(WebApplication.class, args);
 }
}


–新建 model 包,在包下新建⼀个User类:

public class User {
 private String name;
 private int age;
 private String pass;
 //setter、getter省略
}


–在项⽬中新建 Web 包,并在 Web 包下新建⼀个类 WebController,在类中创建⼀个⽅法返回 User:

package com.example.demo;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class WebController {
    @RequestMapping(value="/getUser", method= RequestMethod.POST)
    public User getUser() {
        User user=new User();
        user.setName("小明");
        user.setAge(12);
        user.setPass("123456");
        return user;
    }
}


–在 test 包下新建 WebControllerTest 测试类,对 getUser()测试。

package com.example.demo;
import org.springframework.boot.test.context.SpringBootTest;
import org.junit.Before;
import org.junit.Test;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
@SpringBootTest
public class WebControllerTest {
    private MockMvc mockMvc;

    @Before
    public void setUp() throws Exception {
        mockMvc = MockMvcBuilders.standaloneSetup(new WebController()).build();
    }

    @Test
    public void getUser() throws Exception {
        String responseString = mockMvc.perform(MockMvcRequestBuilders.post("/getUser"))
                .andReturn().getResponse().getContentAsString();
        System.out.println("result : "+responseString);
    }
}

执行Junit Test:
在这里插入图片描述
说明 Spring Boot ⾃动将 User 对象转成了 JSON 进⾏返回。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot是一个基于Spring框架的快速开发框架,它提供了很多便捷的配置和快速的开发环境。在Spring Boot中,我们可以使用@RestController注解来创建一个RESTful风格的Web服务,它支持返回JSON格式的数据。 JSON是一种轻量级的数据交换格式,由于其简单、易读、易解析的特点,被广泛应用于Web应用程序中。在Spring Boot中,我们可以通过以下步骤来实现JSON数据格式化: 1. 导入相关依赖 在pom.xml文件中添加以下依赖: ``` <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> </dependency> ``` 2. 创建一个Controller类 在Controller类中定义一个返回JSON格式数据的接口,例如: ``` @RestController @RequestMapping("/user") public class UserController { @GetMapping("/{id}") public User getUser(@PathVariable Long id) { User user = new User(); user.setId(id); user.setName("张三"); user.setAge(20); return user; } } ``` 其中,@RestController注解表示该类是一个RESTful风格的控制器类,@RequestMapping注解表示请求的路径为"/user",@GetMapping注解表示该接口是一个GET请求。 3. 创建一个实体类 在实体类中定义需要返回的数据,例如: ``` public class User { private Long id; private String name; private Integer age; // getter和setter方法省略 } ``` 4. 测试接口 启动应用程序,并访问"http://localhost:8080/user/1",可以看到返回的数据是一个JSON格式的字符串,例如: ``` {"id":1,"name":"张三","age":20} ``` 以上就是基于Spring Boot框架实现JSON数据格式化的过程。通过使用@RestController注解,我们可以快速地创建一个RESTful风格的Web服务,并返回JSON格式的数据,从而方便地与前端进行数据交互
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值