restful API入门之HelloWorld

<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> </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> <!-- 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>一、用STS创建了一个maven项目,pom.xml如上所示,右键pom.xml并refresh。(更新后有的jar包会下载出错,导致cannot read as zip file类似的错误,需要删掉重新更新一下项目) 

二、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);
    }
}

启动的时候,有报错:

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
https://blog.csdn.net/Loser100/article/details/78190703?locationNum=9&fps=1


参考

问题解决:

https://blog.csdn.net/qq_34448345/article/details/78646435

https://www.cnblogs.com/gczr/p/6692054.html

helloworld参考:

https://blog.csdn.net/john548/article/details/52312637


https://blog.csdn.net/LS7011846/article/details/77769252

猜你喜欢

转载自blog.csdn.net/dongyuguoai/article/details/80963456