0

I receive the following error message:

Error resolving template [catalog/getCatalogItemFromCatalog/catalogItemId/3916677], template might not exist or might not be accessible by any of the configured Template Resolvers

I am trying to reach my service and the method using this url:

 http://192.168.99.100:31003/catalog/getCatalogItemFromCatalog/catalogItemId/3916677

Controller:

@Controller
@RequestMapping("catalog")
public class CatalogController {

    @GetMapping("/getCatalogItemFromCatalog/catalogItemId/{catalogItemId}")
    public CatalogItem getCatalogItemFromCatalog(@PathVariable Integer catalogItemId){
        List<Catalog> catalogs = getAllCatalogs();
        Optional<CatalogItem> optionalCatalogItem = Optional.empty();
        for(Catalog catalog : catalogs){
            optionalCatalogItem = catalog.getCatalogItems().stream().filter(it -> it.getCatalogItemId().equals(catalogItemId)).findFirst();
        }
        return optionalCatalogItem.orElse(null);
    }

    @GetMapping("/system/ipaddr")
    public String getIpAddr() {
        List<String> response;
        response = runSystemCommandAndGetResponse(IP_ADDR);
        return new Gson().toJson(response);
    }
}

When I curl

http://192.168.99.100:31003/catalog/system/ipaddr

I have no issues. I am testing for hours now and nothing seems to work, I have no idea why its failing tho.

elp
  • 840
  • 3
  • 12
  • 36
  • try to adding and slash / @RequestMapping("/catalog") let me know what happens – Jonathan JOhx Dec 23 '18 at 16:52
  • I think I just figured out.. I have no idea why, but `return optionalCatalogItem.orElse(null);` seems to trigger a thymeleaf template. The url mapping works fine, the error message is confusing tho.. Gonna confirm this in a minute. – elp Dec 23 '18 at 16:56
  • Okay then perhaps this answer https://stackoverflow.com/questions/31944355/error-resolving-template-index-template-might-not-exist-or-might-not-be-acces helps you – Jonathan JOhx Dec 23 '18 at 16:58
  • Uhm, this is a little bit weird. But after adding `@ResponseBody` its working.... Someone can explain this to me? – elp Dec 23 '18 at 17:02
  • 1
    Then if you put @RestController instead of Controller.. should work... – Jonathan JOhx Dec 23 '18 at 17:19

1 Answers1

2

you have @Controller on your class which means spring will try to resolve the return type of all your methods inside the controller using all the available templateResolvers.

by using @ResponseBody spring will wrap the return type inside the response (after converting it) directly then returns it to the client, it's similar to using @RestController instead @Controller

stacker
  • 4,317
  • 2
  • 10
  • 24
  • 1
    Now I understand why it's called `Rest`Client ;D Because it's working for clients requesting the data via rest.... :facepalm: Jesus, this took me years xd – elp Dec 23 '18 at 18:55