spring引导:将打印控制台更改为json rest

spring引导:将打印控制台更改为json rest,json,spring,rest,spring-boot,spring-data,Json,Spring,Rest,Spring Boot,Spring Data,实际上,我在Spring中的项目通过控制台从数据库发送值,如下所示: ,但我想像RESTAPI一样通过JSON发送这些值,但我不知道如何更改 { "depositarios": { "correo": "correo", "nombre": "nombre", "numTel": "numTel", "pApellido": "pApellido", "SApellido": "sAellido" } } 这是我的主要课程: @SpringBootApplication @Componen

实际上,我在Spring中的项目通过控制台从数据库发送值,如下所示: ,但我想像RESTAPI一样通过JSON发送这些值,但我不知道如何更改

{ 
"depositarios": {
"correo": "correo",
"nombre": "nombre",
"numTel": "numTel",
"pApellido": "pApellido",
"SApellido": "sAellido"
}
}
这是我的主要课程:

@SpringBootApplication

@ComponentScan("com.abner.springpostgresql.service.impl, com.abner.springpostgresql.dao.imp")
public class SpringPostgresqlApplication {

    public static void main(String[] args) {
        ApplicationContext context= SpringApplication.run(SpringPostgresqlApplication.class, args);
        depoService depoService =context.getBean(depoService.class);
        depoService.loadAllDepo();
    }
}

这是我的整个项目源

您必须使用
@RestController
注释创建一个RestController,如下所示:

@RestController
public class MyRestController {

    @RequestMapping(value = "/personas", method = RequestMethod.GET)
    public List<Persona> listaPersonas() {

        // This is just a sample. Here you could bring your data form your db
        List<Persona> lista = new ArrayList<Persona>();

        Persona p = new Persona();
        p.setNombre("angel");
        p.setEdad(20);
        lista.add(p);

        return lista;
    }
}
@RestController
公共类MyRestController{
@RequestMapping(value=“/personas”,method=RequestMethod.GET)
公开名单{
//这只是一个示例。在这里,您可以从数据库中获取数据
List lista=new ArrayList();
人物角色p=新人物角色();
p、 天使;
p、 setEdad(20);
增加(p);
返回列表a;
}
}
@RequestMapping注释的值(本例中为“/personas”)将是端点。因此,当您访问端点
http://localhost:8080/personas
(假设你的应用程序正在运行)那么你将以json的形式获取数据

这是一个如何做到这一点的例子


是另一个可以帮助您的示例(en-español)。

您可以使用ObjectMapper将pojo或对象转换为JSON字符串,并使用它们的API或任何东西发送到任何需要的地方

或者您可以创建Rest方法并访问API,API将返回Json值

@RestController
public class MyRestController {


@RequestMapping(value = "/depo", method = RequestMethod.GET)
public List<?> getDepo() {

  ApplicationContext context= SpringApplication.run(SpringPostgresqlApplication.class, args);
    depoService depoService =context.getBean(depoService.class);
    List<?> lista = depoService.loadAllDepo();
    return lista;
}
@RestController
公共类MyRestController{
@RequestMapping(value=“/depo”,method=RequestMethod.GET)
公共列表getDepo(){
ApplicationContext context=SpringApplication.run(SpringPostgresqlApplication.class,args);
depoService=context.getBean(depoService.class);
List lista=depoService.loadAllDepo();
返回列表a;
}
另一种方式

@RestController
public class MyRestController {


@RequestMapping(value = "/depo", method = RequestMethod.GET)
public List<Depo> getDepo() {

  ApplicationContext context= SpringApplication.run(SpringPostgresqlApplication.class, args);
    depoService depoService =context.getBean(depoService.class);
    List<Depo> lista = depoService.loadAllDepo();
    return lista;
}
@RestController
公共类MyRestController{
@RequestMapping(value=“/depo”,method=RequestMethod.GET)
公共列表getDepo(){
ApplicationContext context=SpringApplication.run(SpringPostgresqlApplication.class,args);
depoService=context.getBean(depoService.class);
List lista=depoService.loadAllDepo();
返回列表a;
}

启动服务器后,您可以通过执行localhost:8080/depo来运行此操作。您也可以返回XML。

但是您希望在控制台中将对象打印为json,或者将json作为REST API返回?@JuanCarlosMendoza作为RESTAPI@JuanCarlosMendoza我用源项目更新了我的问题。我在你的源代码中没有看到控制器。你看到了吗请检查我的答案并尝试该解决方案?我一直在遵循这一点,我在控制台中收到了这一点,我的本地主机:8080删除了一个错误404。请显示您的控制器和您试图访问的URL抱歉,我没有更新我的github,这里是,我只收到了您获得值“lista”的地方在返回中?更新了我的答案。我从tomcat收到的唯一答案是错误404。请共享您的代码,这是什么类型的应用程序?是spring boot还是常规rest服务?这是,我的应用程序是spring boot类型。