【问题标题】:spring boot: change print console to json restspring boot:将打印控制台更改为 json rest
【发布时间】:2018-01-16 09:44:17
【问题描述】:

实际上,我在 Spring 中的项目通过控制台从数据库发送值,如下所示: Console image,但我想像其他 API 一样通过 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();
    }
}

这是我完整的项目来源https://github.com/abnercoronado/restpostgresql

【问题讨论】:

  • 但是您想在控制台中将对象打印为 json 还是将 json 作为 REST API 返回?
  • @JuanCarlosMendoza 作为 REST API
  • @JuanCarlosMendoza 我用源项目更新了我的问题。
  • 我在您的源代码中没有看到控制器。您是否检查了我的答案并尝试了该解决方案?
  • 我一直在关注这个example

标签: json spring rest spring-boot spring-data


【解决方案1】:

你必须像这样使用@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;
    }
}

@RequestMapping 注解(本例中为“/personas”)的值将是端点。因此,当您访问端点 http://localhost:8080/personas(假设您的应用程序在 http://localhost:8080 上运行)时,您将获得 json 格式的数据。

Here 是一个示例。

Here 是另一个可以帮助你的例子(en español)。

【讨论】:

  • 我在控制台 link 中收到了这个,我的 localhost:8080 丢弃了一个错误 404。
  • 请显示您的控制器和您尝试访问的 URL
  • 抱歉我没有更新我的github,这里是my controller,我只收到了这个error message
【解决方案2】:

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

或者您可以创建 Rest 方法并访问 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
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;
}

一旦你启动了你的服务器,你就可以通过 localhost:8080/depo 来运行它。你也可以返回 XML。

【讨论】:

  • 返回值“lista”在哪里得到?
  • 更新了我的答案。
  • 我从tomcat收到的唯一答案是错误404。
  • 请分享您的代码,这是什么类型的应用程序?是弹簧靴还是定期休息服务?
  • 这是my controller,我的应用是spring boot类型。
猜你喜欢
  • 1970-01-01
  • 2019-12-15
  • 2018-04-15
  • 2019-08-15
  • 1970-01-01
  • 1970-01-01
  • 2018-07-28
  • 2016-04-10
  • 1970-01-01
相关资源
最近更新 更多