項目目錄以下:html
<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>guo</groupId>
<artifactId>xw</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>xw</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>1.3.3.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
server: port: 8092
@Configuration //經過該註解來代表該類是一個Spring的配置,至關於一個xml文件
@ComponentScan(basePackages = "guo.xw")
public class SpringConfig {
@Bean // 經過該註解來代表是一個Bean對象,至關於xml中的<bean>
public UserDao getUserDAO(){
return new UserDao(); // 直接new對象作演示
}
}
@SpringBootApplication
public class App
{
public static void main( String[] args )
{
System.out.println( "Hello World!" );
SpringApplication.run(App.class, args);
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);
UserService userService = context.getBean(UserService.class);
List<User> list = userService.queryUserList();
for (User user : list) {
System.out.println(user.getName()+ ", " + user.getAge());
}
context.destroy();
}
}
@RestController
public class UserController {
@RequestMapping(path="/user" ,method=RequestMethod.GET)
public String testUser(){
return "{ 'name': guoxw, 'age': 10 }";
}
@RequestMapping(path="/userUpdate" , method=RequestMethod.POST)
public User userPost(@RequestBody User user ) {
int age=user.getAge()*2;
String name=user.getName()+"_update";
User user2=new User();
user2.setAge(age);
user2.setName(name);
return user2;
}
}
@RequestMapping(path="/", method=RequestMethod.GET)
public String Test(@RequestParam(name="name", required=true) String name) {
logger.debug("Parameter is: {}", name);
return "Test123" + name;
}
// http://localhost:8092/test/user/guoxw_parameter
@RequestMapping(path="/user/{name}", method=RequestMethod.GET) public User TestUser(@PathVariable String name) { logger.debug("Parameter is: {}", name); User user = new User(); user.setName(name); user.setAge(10); return user; }
static 文件下放置html 以及 上傳保存的圖片。(static 目錄應該在resources 下面 。上圖中位置是不對,不過也不影響運行)java
html文件web
<!DOCTYPE HTML>
<html>
<head>
<title>pictureUploading</title>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8 ">
</head>
<body>
<form enctype="multipart/form-data" method="post" action="/upload">
文件:<input type="file" name="fileUpload"/>
<input type="submit" value="上傳"/>
</form>
</body>
</html>
FileControllerspring
@RestController
public class FileController {
@RequestMapping(path="/upload" , method=RequestMethod.POST)
public Object upload(MultipartFile fileUpload){
//獲取文件名
String fileName = fileUpload.getOriginalFilename();
//獲取文件後綴名
String suffixName = fileName.substring(fileName.lastIndexOf("."));
//從新生成文件名
fileName = UUID.randomUUID()+suffixName;
//指定本地文件夾存儲圖片
String filePath = "D:/eclipseWorkSpace/SpringTestOne/xw/static/";
try {
//將圖片保存到static文件夾裏
fileUpload.transferTo(new File(filePath+fileName));
return new Massage(0,"success to upload");
} catch (Exception e) {
e.printStackTrace();
return new Massage(-1,"fail to upload");
}
}
}
運行如:apache
選擇文件後,點擊上傳,則返回上傳結果信息。app
能夠看到上傳的圖片框架
只是熟悉下Spring boot框架,其中的具體原理和細節都不太清楚,後續再整理學習。dom
code:(Spring boot 初步接觸 (1) )eclipse
連接: https://pan.baidu.com/s/1_qNHW3qwxnXMMmiKg4hJ7A 提取碼: gjjj