我用的请求方法也不是get方法,这是咋回事呢?

来源:4-4 Spring Cloud Alibaba使用场景

慕少3162306

2021-09-13

Service
ResponseEntity userDTO = this.restTemplate.postForEntity(
https://localhost:8080/users/{id}”,
JSON.toJSONString(userId),
UserDTO.class
);
if (“200”.equals(userDTO.getStatusCode())){
UserDTO users = userDTO.getBody();
ShareDTO.builder()
.id(share.getId())
.userId(share.getUserId())
.wxNickname(users.getWxNickname())
.build();
}

Controller
@RequestMapping(value = “/{id}”, method = RequestMethod.POST)
public ShareDTO findById(@PathVariable Integer id) throws Exception {
return this.shareService.findById(JSON.toJSONString(id));
}

http://localhost:8010/shares/1 报错
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.

Mon Sep 13 15:36:01 CST 2021
There was an unexpected error (type=Method Not Allowed, status=405).
后台代码只有这一行是什么问题?
2021-09-13 15:36:01.809 WARN 16796 — [nio-8010-exec-1] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.web.HttpRequestMethodNotSupportedException: Request method ‘GET’ not supported]

写回答

1回答

大目

2021-09-13

Controller

@RequestMapping(value = “/{id}”, method = RequestMethod.POST)
public ShareDTO findById(@PathVariable Integer id) throws Exception {
return this.shareService.findById(JSON.toJSONString(id));
}

这一段是内容中心的代码吧?

你是用的POST请求去访问的吗?你方法上用的是POST啊,如果直接用浏览器访问的话,浏览器会用GET方法请求,所以报错,根本不会进入到方法体里面,更不用说去执行shareService里面的代码了。。

将其改成

@RequestMapping(value = “/{id}”, method = RequestMethod.GET)

试试

0
0

Spring Cloud Alibaba微服务从入门到进阶

面向未来微服务:熟练掌握Spring Cloud Alibaba

3129 学习 · 1327 问题

查看课程