037-案例17 springMVC 常用注解

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

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值