< dependencies>
< dependency>
< groupId> org.springframework.boot< /groupId>
< artifactId> spring-boot-starter-web< /artifactId>
< /dependency>
< dependency>
< groupId> org.springframework.boot< /groupId>
< artifactId> spring-boot-devtools< /artifactId>
< scope> runtime< /scope>
< optional> true< /optional>
< /dependency>
< dependency>
< groupId> org.projectlombok< /groupId>
< artifactId> lombok< /artifactId>
< optional> true< /optional>
< /dependency>
< dependency>
< groupId> org.springframework.boot< /groupId>
< artifactId> spring-boot-starter-test< /artifactId>
< scope> test< /scope>
< /dependency>
< parent>
< groupId> org.springframework.boot< /groupId>
< artifactId> spring-boot-starter-parent< /artifactId>
< version> 2.3 .2.RELEASE< /version>
< relativePath/> < ! -- lookup parent from repository -->
< /parent>
< ?xml version = "1.0" encoding = "UTF-8" ?>
< 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 https://maven.apache.org/xsd/maven-4.0.0.xsd" >
< modelVersion> 4.0 .0 < /modelVersion>
< parent>
< groupId> org.springframework.boot< /groupId>
< artifactId> spring-boot-starter-parent< /artifactId>
< version> 2.3 .2.RELEASE< /version>
< relativePath/> < ! -- lookup parent from repository -->
< /parent>
< groupId> com.java1234 < /groupId>
< artifactId> java1234-mall-v3 < /artifactId>
< version> 0.0 .1-SNAPSHOT< /version>
< name> java1234-mall-v3 < /name>
< description> Demo project for Spring Boot< /description>
< properties>
< java.version> 1 .8 < /java.version>
< /properties>
< dependencies>
< dependency>
< groupId> org.springframework.boot< /groupId>
< artifactId> spring-boot-starter-web< /artifactId>
< /dependency>
< dependency>
< groupId> org.springframework.boot< /groupId>
< artifactId> spring-boot-devtools< /artifactId>
< scope> runtime< /scope>
< optional> true< /optional>
< /dependency>
< dependency>
< groupId> mysql< /groupId>
< artifactId> mysql-connector-java< /artifactId>
< scope> runtime< /scope>
< /dependency>
< ! -- 连接池 -->
< dependency>
< groupId> com.alibaba< /groupId>
< artifactId> druid< /artifactId>
< version> 1.1 .10 < /version>
< /dependency>
< ! -- mybatis-plus -->
< dependency>
< groupId> com.baomidou< /groupId>
< artifactId> mybatis-plus-boot-starter< /artifactId>
< version> 3.3 .2 < /version>
< /dependency>
< dependency>
< groupId> org.projectlombok< /groupId>
< artifactId> lombok< /artifactId>
< optional> true< /optional>
< /dependency>
< ! -- 添加Httpclient支持 -->
< dependency>
< groupId> org.apache.httpcomponents< /groupId>
< artifactId> httpclient< /artifactId>
< version> 4.5 .2 < /version>
< /dependency>
< dependency>
< groupId> com.alibaba< /groupId>
< artifactId> fastjson< /artifactId>
< version> 1.2 .40 < /version>
< /dependency>
< ! -- JWT -->
< dependency>
< groupId> com.auth0 < /groupId>
< artifactId> java-jwt< /artifactId>
< version> 3.2 .0 < /version>
< /dependency>
< dependency>
< groupId> io.jsonwebtoken< /groupId>
< artifactId> jjwt< /artifactId>
< version> 0.7 .0 < /version>
< /dependency>
< dependency>
< groupId> jdom< /groupId>
< artifactId> jdom< /artifactId>
< version> 1 .1 < /version>
< /dependency>
< dependency>
< groupId> dom4j< /groupId>
< artifactId> dom4j< /artifactId>
< version> 1.6 .1 < /version>
< /dependency>
< dependency>
< groupId> commons-io< /groupId>
< artifactId> commons-io< /artifactId>
< version> 2 .5 < /version>
< /dependency>
< /dependencies>
< build>
< plugins>
< plugin>
< groupId> org.springframework.boot< /groupId>
< artifactId> spring-boot-maven-plugin< /artifactId>
< configuration>
< excludes>
< exclude>
< groupId> org.projectlombok< /groupId>
< artifactId> lombok< /artifactId>
< /exclude>
< /excludes>
< /configuration>
< /plugin>
< /plugins>
< /build>
< /project>
yml
server:
port: 8080
servlet:
context-path: /
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/db_java1234_mall_v3?serverTimezone= Asia/Shanghai
username: root
password: 123456
工具类
package com.java1234.entity;
import java.util.HashMap;
import java.util.Map;
/**
* 页面响应entity
* @author java1234_小锋
* @site www.java1234.com
* @company Java知识分享网
* @create 2019 -08-13 上午 10 :00
*/
public class R extends HashMap< String, Object> {
private static final long serialVersionUID = 1L;
public R ( ) {
put( "code" , 0 ) ;
}
public static R error ( ) {
return error( 500 , "未知异常,请联系管理员" ) ;
}
public static R error( String msg) {
return error( 500 , msg) ;
}
public static R error( int code, String msg) {
R r = new R( ) ;
r.put( "code" , code) ;
r.put( "msg" , msg) ;
return r;
}
public static R ok( String msg) {
R r = new R( ) ;
r.put( "msg" , msg) ;
return r;
}
public static R ok( Map< String, Object> map) {
R r = new R( ) ;
r.putAll( map) ;
return r;
}
public static R ok ( ) {
return new R( ) ;
}
public R put( String key, Object value) {
super.put( key, value) ;
return this;
}
}
测试Controller
package com.java1234.controller;
import com.java1234.entity.R;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* 测试
*/
@RestController
@RequestMapping( "/java1234" )
public class TestController {
/**
* 测试
* @return
*/
@GetMapping( "/test" )
public R test ( ) {
return R.ok( "流弊java1234" ) ;
}
}
一、在Spring中@RestController的作用等同于@Controller + @ResponseBody。
所以想要理解@RestController注解就要先了解@Controller和@ResponseBody注解。
二、@Controller注解
在一个类上添加@Controller注解,表明了这个类是一个控制器类。这里省略对Controller注解的说明了。
三、@ResponseBody注解
@ResponseBody表示方法的返回值直接以指定的格式写入Http response body中,而不是解析为跳转路径。
格式的转换是通过HttpMessageConverter中的方法实现的,因为它是一个接口,因此由其实现类完成转换。
如果要求方法返回的是json格式数据,而不是跳转页面,可以直接在类上标注@RestController,而不用在每个方法中标注@ResponseBody,简化了开发过程。
@Controller和@RestController的区别:
@Controller:在对应的方法上,视图解析器可以解析return 的jsp,html页面,并且跳转到相应页面
若返回json等内容到页面,则需要加@ResponseBody注解
@RestController:相当于@Controller+@ResponseBody两个注解的结合,返回json数据不需要在方法前面加@ResponseBody注解了,但使用@RestController这个注解,就不能返回jsp,html页面,视图解析器无法解析jsp,html页面
java: 错误: 无效的源发行版:16
java: 无效的目标发行版: 21
< properties>
< ! -- < java.version> 2 1 < /java.version> -->
< java.version> 1 .8 < /java.version>
< /properties>
更改
测试成功