Spring Boot的Rest API和Web Service

时间:2016-06-27 16:27:28

标签: java web-services rest spring-boot

我有弹簧启动应用程序,使用jdbctemplate我可以使用此URL显示我的数据

http://localhost:8080/query

和这样的结果

[{"id_data":1,"id_user":1,"time":"Thursday, April 09, 2015 18:09:26","ecgvalue":3.3871,"inputtime":"2015-04-09 18:11:25.0"},{"id_data":2,"id_user":1,"time":"Thursday, April 09, 2015 18:09:26","ecgvalue":1.56892,"inputtime":"2015-04-09 18:11:25.0"},{"id_data":3,"id_user":1,"time":"Thursday, April 09, 2015 18:09:26","ecgvalue":1.60802,"inputtime":"2015-04-09 18:11:26.0"},{"id_data":4,"id_user":1,"time":"Thursday, April 09, 2015 18:09:26","ecgvalue":2.09677,"inputtime":"2015-04-09 18:11:26.0"},{"id_data":5,"id_user":1,"time":"Thursday, April 09, 2015 18:09:26","ecgvalue":1.99902,"inputtime":"2015-04-09 18:11:26.0"}]

我知道那是json对象。我的问题是如何从中提供网络服务?喜欢Rest Web服务 这是我的代码 QueryController.java

package com.ewsn.eepiscure.controller;

/**
 *
 * @author sammy
 */
import java.util.List;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class QueryController {

    @Autowired
    protected JdbcTemplate hiveTemplate;

    @RequestMapping("/query")

    public List query() {
        List data = hiveTemplate.queryForList("select * from ecg.hivetbluserdata limit 100");
        return data;
    }


}

1 个答案:

答案 0 :(得分:2)

如果您对使用Spring在Java中创建“REST Web服务”感兴趣,我建议您查看允许创建的Spring Data Rest模块

  

在Spring Data存储库之上的超媒体驱动的REST Web服务

请参阅http://projects.spring.io/spring-data-rest/