当前位置:网站首页>记一个RestControll和Controller 引起的折磨BUG
记一个RestControll和Controller 引起的折磨BUG
2022-06-26 01:27:00 【wow_awsl_qwq】
项目中用的Controller ,然后请求路径无误的情况下,请求/account/list 结果给浏览器返回404,但是调试时却可以获取到信息,即数据库查询无误,仅仅到了返回给浏览器这一步就出错了
解决
后来发现是Controller注解和RestController注解的区别导致的问题
RestController = Controller +
ResponseBody,例如在项目当中,你加上的是RestController,那么返回的内容是你return中的内容,如果是return
“Hello World”,页面显示的就是Hello
World。但是如果你加上Controller,返回的是return中对应的页面,比如return
“hello”,页面的名称是hello,此时如果你的项目中没有hello这个页面,那么必然会出现404的错误
所以如果注解是Controller,那么它返回的是一个map,浏览器把它当做一个路径了,自然是找不到资源,而如果是RestController,相当于加了ResponseBody注解,浏览器会将它当成是内容,以上
@Controller//错误,应该改成@RestController
@RequestMapping("/account")
public class AccountController {
@Autowired
private AccountService accountService;
@RequestMapping("/list")
public Map<String,Object> listPageAccounts(Integer page, Integer rows, String aname){
Map<String,Object> map=new HashMap<>();
map.put("total",accountService.countAccounts(aname));
map.put("rows",accountService.listPageAccounts(page,rows,aname));
return map;
}
@RequestMapping("/saveAccount")
public boolean saveAccount(Account account, Category category){
account.setCategory(category);
return accountService.saveAccount(account);
}
}
边栏推荐
- 哪个证券公司手机股票开户更好更安全?
- Is it safe to trade stocks with a compass? How does the compass trade stocks? Do you need to open an account
- Blazor University (33)表单 —— EditContext、FieldIdentifiers
- CVPR2022 | 长期行动预期的Future Transformer
- 基於鄰接矩陣的廣度優先遍曆
- Back to top case
- SQL column value to row value (unpivot)
- Breadth first traversal based on adjacency table
- Implement decorator pattern for servicecollection
- Redis-SDS
猜你喜欢
Ardiuno smart mosquito racket
官方零基础入门 Jetpack Compose 的中文课程来啦!
College Students' big factory road
Markov decision process (MDP): gambler problem
One year's work
MySQL必須掌握4種語言!
Eureka注册信息配置备忘
Audio video and CPU architecture
How do I fix the iPhone green screen problem? Try these solutions
深度好文:什么是超网 Supernetting?
随机推荐
heaven and hell's moving
微服务之consul
OA process editing
Digital commodity DGE -- the dark horse of wealth in digital economy
leetcode 300. Longest increasing subsequence (medium)
Binary search
What is flush software? Is online account opening safe?
Tab switch
【图像过滤】基于matlab GUI图像过滤系统【含Matlab源码 1913期】
基於鄰接矩陣的廣度優先遍曆
Problem solving process of no internet access
Depth first traversal based on adjacency table
. Net7 miniapi (special part):preview5 optimizes JWT verification (Part 2)
Is it safe to trade stocks with a compass? How does the compass trade stocks? Do you need to open an account
Wechat launched a web version transmission assistant. Is it really easy to use?
Sloppy formula
Markov decision process (MDP): Blackjack problem (mc-es)
Tarte aux framboises + AWS IOT Greengrass
MySQL必须掌握4种语言!
程序员的八年工资变动,令网友羡慕不已:你一个月顶我一年工资