新手看看,大牛醬油~java
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.aspirecn.exer</groupId> <artifactId>webfw</artifactId> <version>1.0.0</version> <!-- springboot通常引用jar就行 --> <packaging>jar</packaging> <!-- 設置父引用 --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.1.RELEASE</version> </parent> <!-- 配置須要的各個jar包版本、JAVA版本信息 --> <properties> <java.version>1.8</java.version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <!-- 所須要的依賴包 --> <dependencies> <!-- web SpringMvc、AOP等依賴的包 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> </project>
package com.springboot.entity public class Demo { private int id; //建立id private String name; //建立名字 //Springboot默認調用了jackson包,能夠用此註解格式化日期格式 @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss") private Date createTime; //建立時間 private String remarks; //備註信息 public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Date getCreateTime() { return createTime; } public void setCreateTime(Date createTime) { this.createTime = createTime; } public String getRemarks() { return remarks; } public void setRemarks(String remarks) { this.remarks = remarks; } }
package com.springboot.controller; import java.util.Date; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; //@RestController = @ResponseBody + @Controller組合 @RestController public class HelloController { //若是發生亂碼,能夠改爲 //@RequestMapping(path="/getDemo",produces = "application/json;charset=UTF-8" ) @RequestMapping("/getDemo") public Demo getDemo(){ Demo demo=new Demo(); demo.setId(1); demo.setName("李四"); demo.setCreateTime(new Date()); demo.setRemarks("這是備註信息"); return demo; } }
package com.springboot; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; //加此註解說明此爲Springboot啓動類 @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Appication.class, args); System.out.println("_____啓動完成______"); } }
直接啓動Application運行web
輸入:spring
http://localhost:8080/getDemoapache
運行結果:json