spring boot开发restful API入门(一)之HelloWorld

一、整体结构(创建maven项目,下面结构中有一些多余文件,可忽略)

 一、编写pom.xml

<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.gyd</groupId>
  <artifactId>moneyCom</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>moneyCom</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  <jackson.version>2.8.11</jackson.version>
  </properties>
<parent>  
        <groupId>org.springframework.boot</groupId>  
        <artifactId>spring-boot-starter-parent</artifactId>  
        <version>1.5.3.RELEASE</version>  
    </parent>
  
  <dependencies> 
	    
	    
        <!-- Compile -->  
        <dependency>  
            <groupId>org.springframework.boot</groupId>  
            <artifactId>spring-boot-starter-web</artifactId>  
        </dependency>  
  
  		<!-- 使用的数据库是MySql -->
        <dependency>  
            <groupId>mysql</groupId>  
            <artifactId>mysql-connector-java</artifactId>  
        </dependency>  
  
  		<!-- 使用JDBC访问数据库 -->
        <dependency>    
            <groupId>org.springframework.boot</groupId>  
            <artifactId>spring-boot-starter-jdbc</artifactId>  
        </dependency>  
  
  		<!-- 使用jpa处理数据 -->
        <dependency>  
            <groupId>org.springframework.boot</groupId>  
            <artifactId>spring-boot-starter-data-jpa</artifactId>  
        </dependency>  
  
         <dependency>  
            <groupId>com.h2database</groupId>  
            <artifactId>h2</artifactId>  
            <scope>runtime</scope>  
        </dependency>  
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-core</artifactId>
      <version>${jackson.version}</version>
    </dependency>
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-databind</artifactId>
      <version>${jackson.version}</version>
    </dependency>
        <!-- Test -->   
        <dependency>   
            <groupId>org.springframework.boot</groupId>   
            <artifactId>spring-boot-starter-test</artifactId>   
            <scope>test</scope>   
        </dependency>   
      <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>  
  
  
  <build>  
        <plugins>   
            <plugin>  
            	<!-- jar打包用 -->  
                <groupId>org.springframework.boot</groupId>   
                <artifactId>spring-boot-maven-plugin</artifactId>   
            </plugin>   
            <plugin>   
                <groupId>org.apache.maven.plugins</groupId>   
                <artifactId>maven-surefire-plugin</artifactId>   
            </plugin>   
        </plugins>   
    	<finalName>mono.localhost</finalName>  
  </build>


</project>

二、service类

package com.gyd.moneyCom.service;


import org.springframework.stereotype.Service;

/*
 * 
 * 		service(处理业务逻辑):
 * 			1.@Service:标注为服务类
 * 
 * 
 */

@Service
public class restful_helloWorld_service {

	public void test(String a){
		System.out.println("参数 : "+a);//服务器搞
	}
	
}

三、controller类

package com.gyd.moneyCom.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
 
import com.gyd.moneyCom.service.restful_helloWorld_service;
 
/*
 * 
 * 	控制器:
 * 			1.@RestController:定义控制器
 * 			2.@RequestMapping(
 * 				value = "/login", 
 * 				method = RequestMethod.POST, 
 * 				consumes = "application/json"):url映射/post请求/数据传输为json
 * 			3.@ResponseBody:有响应
 * 
 * 
 * 
 */

@RestController
@RequestMapping("/helloWorld")
public class restful_helloWorld_controller {

	@Autowired
	private restful_helloWorld_service service;
	
    @RequestMapping(value = "/test1",method = RequestMethod.GET,consumes = "application/json")  
    @ResponseBody
	private void test(@RequestParam String a){
    	service.test(a);//这个方法没懂
    }
    
    @RequestMapping(value = "/test2")  
    @ResponseBody
	private void test2(){
    	service.test("无参数");
    }

	
}

四、app类

package com.gyd.moneyCom;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
 
 
/*
 * 		启动类 :
 * 
 * 			1.@SpringBootApplication:标注这个类为启动类,自动包含了下面三个注解
 * 				@Configuration:标注这个类为整个服务的上下文资源
 * 				@EnableAutoConfiguration:指示springBoot开始基于classPath设置,添加其他bean
 * 				@ComponentScan:告诉spring扫描其他组件/服务/配置
 *
 */
 
 


/**
 * Hello world!
 *
 */
@SpringBootApplication
public class App 
{
    public static void main( String[] args )
    {
        System.out.println( "Hello World!" );
        System.out.println( "app !" );
        SpringApplication.run(App.class, args);
    }
}

五、启动

右键App.java,run as, Java Application

六、浏览器中输入以下网址,可以看到服务器打印的处理信息

七、将项目打包成jar包、修改服务端口请继续参考[1]

八、参考

[1]. https://blog.csdn.net/qq_34448345/article/details/78646435  (本文参考)

[2]. https://www.cnblogs.com/gczr/p/6692054.html (application.properties配置文件解析与使用,包括属性bean,参数间调用,自定义配置文件,配置文件优先级等等)

九、问题解决:

1. Cannot determine embedded database driver class for database type NONE

在Application.properties文件内配置数据源即可:

spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.max-idle=10
spring.datasource.max-wait=10000
spring.datasource.min-idle=5
spring.datasource.initial-size=5

2. 参考:

https://blog.csdn.net/Loser100/article/details/78190703?locationNum=9&fps=1

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值