springboot中 转换数据为json数据

package xyz.ark.backstage_08.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import xyz.ark.backstage_08.entity.User;

@RestController
@RequestMapping("/user")
public class UserController {

    ObjectMapper objectMapper = new ObjectMapper();

    @GetMapping("/find")
    public String findUserList() throws JsonProcessingException {
        User user = new User();
        user.setUsername("张三");
        user.setPassword("123");

        String string = objectMapper.writeValueAsString(user);

        return string;
    }

}

发布了33 篇原创文章 · 获赞 23 · 访问量 6608

猜你喜欢

转载自blog.csdn.net/weixin_40516924/article/details/103378677
今日推荐