原文地址:

http://412887952-qq-com.iteye.com/blog/2291508

在做如下操作之前,我们对之前的Hello进行简单的修改,我们新建一个包com.kfit.test.web 然后新建一个类HelloControoler, 然后修改App.java类,主要是的这个类就是一个单纯的启动类。

主要代码如下:

App.java

packagecom.kfit
 
importorg.springframework.boot.SpringApplication;
importorg.springframework.boot.autoconfigure.SpringBootApplication;
/**
 * Hello world!
 *
 */
//其中@SpringBootApplication申明让spring boot自动给程序进行必要的配置,等价于以默认属性使用@Configuration,@EnableAutoConfiguration和@ComponentScan
@SpringBootApplication
publicclassApp {
              publicstatic void main(String[] args) {
                 SpringApplication.run(App.class, args);
       }
}

com.kfit.test.web.HelloController

package com.kfit.test.web;
 
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
@RestController// 标记为:restful
publicclass HelloController {
   
    @RequestMapping("/")
    public String hello(){
       return"Hello world!";
    }
}
运行代码和之前是一样的效果的。

我们在编写接口的时候,时常会有需求返回json数据,那么在spring boot应该怎么操作呢?主要是在class中加入注解@RestController,。

返回JSON之步骤:

   (1)编写一个实体类Demo

   (2)编写DemoController;

   (3)在DemoController加上@RestController和@RequestMapping注解;

   (4)测试

具体代码如下:

package com.kfit.test.bean;
/**
 * 测试实体类.
 * @author Administrator
 *
 */
publicclass Demo {
    privatelongid;//主键.
    private String name;//测试名称.
    publiclong getId() {
       returnid;
    }
    publicvoid setId(longid) {
       this.id = id;
    }
    public String getName() {
       returnname;
    }
    publicvoid setName(String name) {
       this.name = name;
    }
}

package com.kfit.test.web;
 
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
import com.kfit.test.bean.Demo;
 
/**
 * 测试.
 * @author Administrator
 *
 */
@RestController
@RequestMapping("/demo")
publicclass DemoController {
   
    /**
     * 返回demo数据:
     * 请求地址:http://127.0.0.1:8080/demo/getDemo
     * @return
     */
    @RequestMapping("/getDemo")
    public Demo getDemo(){
       Demo demo = new Demo();
       demo.setId(1);
       demo.setName("Angel");
       returndemo;
    }
   
}
那么在浏览器访问地址:http://127.0.0.1:8080/demo/getDemo 返回如下数据:

{
id: 1,
name: "Angel"
}

       是不是很神奇呢,其实Spring Boot也是引用了JSON解析包Jackson,那么自然我们就可以在Demo对象上使用Jackson提供的json属性的注解,对时间进行格式化,对一些字段进行忽略等等。


 

GitHub 加速计划 / js / json
18
5
下载
适用于现代 C++ 的 JSON。
最近提交(Master分支:3 个月前 )
2134cb94 * change NLOHMANN_JSON_FROM_WITH_DEFAULT to let NLOHMANN_DEFINE_TYPE_INTRUSIVE_WITH_DEFAULT work with an empty JSON instance * fix ci_static_analysis_clang (ci_clang_tidy) * change NLOHMANN_JSON_FROM_WITH_DEFAULT to let NLOHMANN_DEFINE_TYPE_INTRUSIVE_WITH_DEFAULT work with an empty JSON instance 7 天前
6057b31d * :wrench: overwork astyle call * :wrench: overwork astyle call * :wrench: overwork astyle call * :wrench: overwork astyle call * :wrench: overwork astyle call * :wrench: overwork astyle call * :wrench: overwork astyle call * :wrench: overwork astyle call * :wrench: overwork astyle call * :wrench: overwork astyle call * :wrench: overwork astyle call * Use ubuntu-latest image to run Valgrind (#4575) * :wrench: use Clang image to run valgrind * :wrench: use Clang image to run valgrind * :wrench: use Clang image to run valgrind * :wrench: use Ubuntu image to run valgrind * Use Clang image to run iwyu (#4574) * :wrench: use Clang image to run iwyu * :wrench: use Clang image to run iwyu * :wrench: overwork astyle call * :wrench: overwork astyle call * :wrench: overwork astyle call * :wrench: overwork astyle call * :wrench: overwork astyle call * :wrench: overwork astyle call * :wrench: overwork astyle call * :wrench: overwork astyle call * :wrench: overwork astyle call * :wrench: overwork astyle call * :wrench: overwork astyle call * :art: format code * :hammer: clean up 9 天前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐