springboot开发教程:使⽤@RestController、@Controller注。。。spring boot使⽤ @Controller注解控制构造常见http get 、post请求案例
项⽬结构如下:
1、maven依赖l 配置
具体作⽤看代码注释。
<?xml version="1.0" encoding="UTF-8"?>thymeleaf用法
<project xmlns="/POM/4.0.0"xsi="/2001/XMLSchema-instance"
schemaLocation="/POM/4.0.0 /xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.yf.springboot</groupId>
<artifactId>spring-boot-myfirst</artifactId>
<version>0.0.1-SNAPSHOT</version>
<!-- Inherit defaults from Spring Boot -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.1.RELEASE</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java-version>1.8</java-version>
</properties>
<!-- Add typical dependencies for a web application -->
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<!-- 不需要version 会根据parent版本进⾏添加上 -->
</dependency>
<!-- 添加fastjson ⽀持 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.15</version>
</dependency>
<!-- 使⽤thymeleaf模板 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!-- 使⽤freemaker模板 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
</dependencies>
<!-- Package as an executable jar -->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<!-- 在这⾥添加springLoader plugin 实现热拔插开发 1、run as maven build ⾥⾯配置 goal:
spring-boot:run 问题控制台maven重启会报错:Verify the connector's configuration, identify
and stop any process that's listening on port 8080, or configure this application
to listen on another port. ⼀般不⽤这种。关闭还会占⽤端⼝ 2、run as -java application 需要把springloaded-1.2.3.RELEASE.jar下载下来。放在⼯程⽬录下然后run-config设置jvm 参数 -javaagent:.\lib\springloaded-1.2.3.RELEASE.jar -noverify -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>springloaded</artifactId>
<version>1.2.3.RELEASE</version>
</dependency>
</dependencies>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
<configuration>
<configuration>
<classifier>exec</classifier>
</configuration>
</plugin>
</plugins>
</build>
</project>
采⽤spring boot主要配置⽂件l,建议使⽤装上spring suit插件会有提⽰,使⽤IDEA也可以有相应插件⽀持。⽤yml做配置⽂件有严格格式和有提⽰⽀持,properties⽂件不太⽅便。
没有配置⽂件,启动的spring boot会默认使⽤8080端⼝。
2、项⽬配置⽂件l
server:
port:8800
spring:
thymeleaf:
prefix: classpath:/templates/
suffix: .html
mode: HTML5
encoding: UTF-8
content-type: text/html; charset=utf-8
# 设置缓存为false 为了热部署 host refresh
cache:false
# 设置freemarker
freemarker:
allow-request-override:false
# 开发过程建议关闭缓存
cache:true
check-template-location:false
charset: UTF-8
content-type: text/html; charset=utf-8
expose-request-attributes:false
expose-session-attributes:false
expose-spring-macro-helpers:false
# prefix: xx
request-context-attribute:
# settings:
# 默认后缀就是.ftl
# suffix: .ftl
# template-loader-path: classPath:/templates/
# view-names:
3、编写启动主类
注意:main类所在包是其他类的顶级包,这样才能能扫描到controller等注解。
注意:这⾥映⼊了第三⽅json处理依赖
package com.yf.springboot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.web.HttpMessageConverters;
import t.annotation.Bean;
import org.verter.HttpMessageConverter;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fig.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
/**
* 出现404解决
* 1、地址是否正确
* 2、注解是否对了
* 3、包路径是否正确,能否被扫描到加载到
* 默认启动8080端⼝
* @Description 这⾥使⽤@SpringBootApplication只是⼀个springBoot应⽤程序
* @author Administrator
* @date 2017-4-21 下午9:08:54
* @version V1.3.1
*/
@SpringBootApplication
public class Application
//extends WebMvcConfigurerAdapter
{
/**
* 添加第三⽅json⼯具
* 1、需要再l加⼊相关以来
* 2、需要再APP 继承 WeWebMvcConfigurerAdapter 重写configureMessageConverters * 3、或者使⽤bean注⼊fastJsonHttpMessageConverters
*
*
* 配置fastjson⽀持两种⽅法
* ⼀:1、启动继承 WebMvcConfigurerAdapter 2、覆盖⽅法configureMessageConverters * ⼆:使⽤bean注⼊fastJsonHttpMessageConverters
* 这⾥使⽤@Bean注⼊ HttpMessageConverters
* @Description
* @author Administrator
* @return
*/
@Bean
public HttpMessageConverters fastJsonHttpMessageConverters(){
//1、定义convert转换消息对象
FastJsonHttpMessageConverter fasConverter =new FastJsonHttpMessageConverter(); //2、添加fastJson的配置信息,⽐如:是否要格式化返回json数据
FastJsonConfig fastJsonConfig =new FastJsonConfig();
fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
//3、再convert中添加配置信息
fasConverter.setFastJsonConfig(fastJsonConfig);
HttpMessageConverter<?> converter = fasConverter;
return new HttpMessageConverters(converter);
}
// 1、需要先定义⼀个conver转换消息对象
// 2、添加fastJson配置信息,⽐如:⾷⾁需要格式化返回json数据
// 3、再convert中添加配置信息
// 4、讲convert添加到converters中
/*@Override
public void configureMessageConverters(
List<HttpMessageConverter<?>> converters) {
FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter(); FastJsonConfig fastJsonConfig = new FastJsonConfig();
fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
fastConverter.setFastJsonConfig(fastJsonConfig);
converters.add(fastConverter);
}*/
public static void main ( String[] args )
{
SpringApplication.run(Application.class, args);
}
}
4、controller类开发HTTP请求接⼝
package com.ller;
package com.ller;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.ity.User;
import com.ity.User2;
/
**
* 注解全名:
* @Description 使⽤RestController 相当于@Controller 和 @RequestBody
* @author Administrator
* @date 2017-4-21 下午9:06:28
* @version V1.3.1
*/
// 相当于 @Controller + @ResponseBody
// 该注解⽅法method 返回类型是String时候则返回string,返回对象时候则讲json_encode 该对象的json字符串@RestController
// @EnableAutoConfiguration
@RequestMapping("/test")
public class HelloController {
/**
* 请求: localhost:8800/test/ http⽅式:get 请求返回contentType: text/plain
* 请求responseBody: "Hello page"
*
* @Description
* @author Administrator
* @return
*/
@RequestMapping("/")
public String index(){
return"Hello page";
}
/**
* 请求:localhost:8800/test/hello/
* http⽅式:get 请求
* 返回contentType: text/plain
* 请求responseBody: "Hello 123 abc 123 123 "
* @Description
* @author Administrator
* @return
*/
@RequestMapping("/hello")
public String hello(){
return"Hello 123 abc 123 123 ";
}
/**
* 请求:localhost:8800/test/hello/testname
* 请求返回contentType:
* responseBody: "Hello testname"
* @Description
* @author Administrator
* @param myName
* @return
*/
@RequestMapping("/hello3/{myName}")
public String hello3(@PathVariable String myName){
return"Hello "+ myName +"";
}
/**
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论