Spring boot(Spring Boot 1.5.7.RELEASE)实践

1.    结构

1.     入口src/main/java

@SpringBootApplication注释: 主程序入口 DemoApplication, 可以通过直接运行该类来启动Spring Boot应用。

 

2.     配置src/main/resources

图片、 css 、JavaScript =》static

Web页面的模板文件 =》templates

3.     单元测试src/test

ü  解决Maven依赖库的问题


1. =》需要设置maven本地仓库 =》settings.xml中修改

C:\\Users\\Administrator\\.m2\\repository

2.eclipse指定settings.xml文件 , 进入Window =》preferances=》Maven=》User Setting指定路径


Spring boot(Spring Boot 1.5.7.RELEASE)实践_第1张图片

hello world

Eclipse创建项目 tcloud-user-provider

 

配置依赖包

<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.0modelVersion>

 

    <groupId>com.svw.tbox.tcloud.usergroupId>

    <artifactId>tcloud-user-providerartifactId>

    <version>0.0.1-SNAPSHOTversion>

    <name>tcloud-user-providername>

    <url>http://maven.apache.orgurl>

 

    <parent>

        <groupId>org.springframework.bootgroupId>

        <artifactId>spring-boot-starter-parentartifactId>

        <version>1.4.3.RELEASEversion>

    parent>

 

    <properties>

        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>

        1.8

    properties>

 

    <dependencies>

        <dependency>

            <groupId>org.apache.maven.pluginsgroupId>

            <artifactId>maven-resources-pluginartifactId>

            <version>2.6version>

        dependency>

        <dependency>

            <groupId>org.springframework.bootgroupId>

            <artifactId>spring-boot-starter-webartifactId>

        dependency>

 

        <dependency>

            <groupId>com.alibabagroupId>

            <artifactId>fastjsonartifactId>

            <version>1.2.15version>

        dependency>

 

        <dependency>

            <groupId>mysqlgroupId>

            <artifactId>mysql-connector-javaartifactId>

            <version>5.1.44version>

        dependency>

        <dependency>

            <groupId>org.mybatis.spring.bootgroupId>

            <artifactId>mybatis-spring-boot-starterartifactId>

            <version>1.2.1version>

        dependency>

        <dependency>

            <groupId>com.github.pagehelpergroupId>

            <artifactId>pagehelperartifactId>

            <version>4.1.0version>

        dependency>

 

        <dependency>

            <groupId>org.springframework.cloudgroupId>

            <artifactId>spring-cloud-starter-eurekaartifactId>

        dependency>

    dependencies>

   

    <dependencyManagement>

        <dependencies>

            <dependency>

                <groupId>org.springframework.cloudgroupId>

                <artifactId>spring-cloud-dependenciesartifactId>

                <version>Camden.SR4version>

                <type>pomtype>

                <scope>importscope>

            dependency>

        dependencies>

    dependencyManagement>

project>

1.     创建Controller类

package com.svw.tbox.tcloud.user.provider.controller;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

 

/**

 * @RestController = @Controller+@RequestBody

 * @author Administrator

 *

 */

@RestController

publicclass UserController {

   /**

    * 建立映射:

    * http://127.0.0.1:8080/hello

    * @return

    */

   @RequestMapping("/hello")

   public String hello(){

     return"你好";

   }

}

 

2.     创建main类

使用@SpringBootApplication指定这是一个SpringBoot应用程序。

package com.svw.tbox.tcloud.user.provider;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

 

@SpringBootApplication

publicclass ProviderApplication {

   publicstaticvoid main(String[] args) {

      SpringApplication.run(ProviderApplication.class, args);

   }

}

3.     运行

在main类中右击->run as -> java application 出现 如下代码 说明运行成功

Started ProviderApplication in 4.522 seconds (JVM running for 5.345)

4.     效果

访问Controller映射地址,出现如下返回结果就运行成功


Spring boot(Spring Boot 1.5.7.RELEASE)实践_第2张图片

1.    对象转json

1.     写model类

packagecom.svw.tbox.tcloud.user.provider.entity;

 

import java.math.BigDecimal;

 

publicclass User {

 

   private Long id;

   private String username;

   private String name;

   private Integer age;

   private BigDecimal balance;

 

   public User() {

      super();

   }

 

   public User(Long id, String username, String name, Integer age, BigDecimal balance) {

      super();

      this.id = id;

      this.username = username;

      this.name = name;

      this.age = age;

      this.balance = balance;

   }

 

   public Long getId() {

      returnthis.id;

   }

 

   publicvoid setId(Long id) {

      this.id = id;

   }

 

   public String getUsername() {

      returnthis.username;

   }

 

   publicvoid setUsername(String username) {

      this.username = username;

   }

 

   public String getName() {

      returnthis.name;

   }

 

   publicvoid setName(String name) {

      this.name = name;

   }

 

   public Integer getAge() {

      returnthis.age;

   }

 

   publicvoid setAge(Integer age) {

      this.age = age;

   }

 

   public BigDecimal getBalance() {

      returnthis.balance;

   }

 

   publicvoid setBalance(BigDecimal balance) {

      this.balance = balance;

   }

}

 

2.     写Controller映射方法

packagecom.svw.tbox.tcloud.user.provider.controller;

import java.math.BigDecimal;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

importcom.svw.tbox.tcloud.user.provider.entity.User;

 

/**

 * @RestController = @Controller+@RequestBody

 * @author Administrator

 *

 */

@RestController

publicclass UserController {

   /**

    * 建立映射:

    * http://127.0.0.1:8000/hello

    * @return

    */

   @RequestMapping("/hello")

   public String hello(){

     return"你好";

   }

  

   @RequestMapping("/getUser")

   public User getUser(){

      returnnew User(1L, "小般般", "胡话", 30, new BigDecimal(10000000.00));

   }

}

 

效果:

Spring boot(Spring Boot 1.5.7.RELEASE)实践_第3张图片





你可能感兴趣的:(spring,boot)