037-案例17 springMVC 常用注解
get,post,delete,put 增删改查
038-案例18 使用RESTful-1
像一种穿衣风格的一种风格!嗯。。
请求方式的写法的一种风格,讲义3.4.1
039-案例18 使用RESTful-2
注意:新建springboot工程时,Artifact那里不允许有大写,但是文件夹名字可以有大写
@RequestMapping(value = "/student/detail/{id}/{age}")
public Object student1(@PathVariable("id") Integer id,
@PathVariable("age") Integer age){
Map<String,Object> retMap=new HashMap<>();
retMap.put("id",id);
retMap.put("age",age);
return retMap;
再启动,如下
改成如下后
@RequestMapping(value = "/student/detail/{id}/{status}")
public Object student2(@PathVariable("id") Integer id,
@PathVariable("status") Integer status){
Map<String,Object> retMap=new HashMap<>();
retMap.put("id",id);
retMap.put("status",status);
return retMap;
}
再启动访问,出问题了
报错原因
java.lang.IllegalStateException: Ambiguous handler methods mapped for ‘/student/detail/1010/28’: {public java.lang.Object com.bjpowernode.springboot.web.StudentController.student1(java.lang.Integer,java.lang.Integer)
问题1:因为下图部分不清楚是该走哪条请求路径
//以上代码student1和student2会出现请求路径迷糊的错误
//通常在RESTful风格中方法的请求方式会按增删改查的请求方式来区分
改成如下图
问题2:如果都是要delete怎么办
还是报错Ambiguous handler methods mapped for ‘/student/detail/1010/28’
改动一下路径顺序
@DeleteMapping(value = "/student/detail/{id}/{status}")
@DeleteMapping(value = "/student/{id}/detail/{city}")
//以上代码student1和student2会出现请求路径迷糊的错误
//通常在RESTful风格中方法的请求方式会按增删改查的请求方式来区分
//修改请求路径(当出现冲突时)
//RESTful请求风格要求路径中使用的单词都是名称,最好不要出现动词
040 案例19-集成redis-1
下载安装redis环境,及desktop,不要忘了设密码,要不然连接会出错
application.properties配置内容写
#设置redis配置信息
spring.redis.host=localhost
spring.redis.port=6379
spring.redis.password=root
但是,我还有一个问题,redis里边reload看不到connection tree,why呢??????
http://localhost:8080/get
但是前端请求报错,后端没报错
controller和impl打断点
前端发送请求,F8执行到断点处
发现取到了1001值,如下图
忘记加@ResponseBody了,加上就好了,就查出来了
@RequestMapping(value = "/get")
public @ResponseBody String get(){
String count=studentService.get("count");
return "数据count为:"+ count;
}
042 案例20-集成dubbo-1
见笔记20
先创建接口工程020,没必要springboot,创建个maven;
021服务提供者为springboot工程;
022服务消费者为springboot工程
043 案例20-集成dubbo-2
application.properties配置文件写dubbo
045 案例20-集成dubbo-4
022工程要引用这个020service,所以这里创建service的时候选择020路径中的,选择如下
022工程实现这个接口来自021,所以把对应的service.impl放在021
StudentServiceImpl.java中加@Component
再@Service,这个注解import的是import com.alibaba.dubbo.config.annotation.Service;
而不再是import org.springframework.stereotype.Service;
@Component//放到接口容器里
@Service(interfaceClass = StudentService.class,version="1.0.0",timeout = 15000)
以上这句话,dubbo所提供的service注解,一句话就可以
以前需要如下这样//dubbo。。。。这种标签写
@Component//放到接口容器里
@Service(interfaceClass = StudentService.class,version="1.0.0",timeout = 15000)
//dubbo:service interface="" version="" timeout=""
同理Reference也是,引用dubbo的
import com.alibaba.dubbo.config.annotation.Reference;
在配置文件处application.properties 加
package com.bjpowernode.springboot;
import com.alibaba.dubbo.spring.boot.annotation.EnableDubboConfiguration;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication//开启spring注解配置
@EnableDubboConfiguration //开启dubbo配置
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}