当前位置:网站首页>Record a torture bug caused by restcontrol and controller

Record a torture bug caused by restcontrol and controller

2022-06-26 02:56:00 wow_ awsl_ qwq

Used in the project Controller , Then the request path is correct , request /account/list The result is returned to the browser 404, However, information can be obtained during debugging , That is, the database query is correct , Just return to the browser and an error occurs

solve
Later it was found that Controller Notes and RestController Differences in annotations cause problems

RestController = Controller +
ResponseBody, For example, in the project , You added RestController, Then the content returned is you return The content in , If it is return
“Hello World”, The page shows Hello
World. But if you add Controller, The return is return The corresponding page in , such as return
“hello”, The name of the page is hello, At this point, if your project does not have hello This page , Then there must be 404 Error of

So if the annotation is Controller, Then it returns a map, The browser takes it as a path , Naturally, there is no resource to be found , And if it is RestController, It's equivalent to adding ResponseBody annotation , Browsers will think of it as content , above

@Controller// error , Should be changed into @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);
    }

}
原网站

版权声明
本文为[wow_ awsl_ qwq]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/177/202206260127255257.html