0

I have a simple controller class exposing two rest endpoints.

Removing the @ResponseBody is giving different behavior for the two endpoints.

If removed from testRest method - output is =

"1..name"
"1..name..name"
"1..name..name..name" 

and so on, finally Stackoverflow exception is thrown

If removed from testSimple method - No Exception , output is "Test simple", response to Postman is :-

{
    "timestamp": "2020-06-09T17:20:02.511+0000",
    "status": 404,
    "error": "Not Found",
    "message": "No message available",
    "path": "/testSimple"
}

Code is :-

package com.search.catalog.rest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class TestController {

    @RequestMapping(value="/test/{id}", method = RequestMethod.GET)
    @ResponseBody
    public String testRest(@PathVariable String id, @RequestParam String name) {

        System.out.println(id + ".." + name);

        return "result is " + id+ ".." + name;
    }



    @RequestMapping(value="/testSimple", method = RequestMethod.GET)
    @ResponseBody
    public String testSimple() {

        System.out.println("Test Simple ");

        return "Test Simple ";
    }
}
Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724

2 Answers2

1

Without @ResponseBodyspring looks for ViewResolver and as you haven't declared any so spring will use default one InternalResourceViewResolver

By calling ../test/1?name="name" you are experiencing Circular view path .i.e try replacing return "result is " + id + ".." + name; with return "result is "; in testRest().

Answer given by @Sotirios Delimanolis will throw more light on Circular view path.

But if you use ../test/1/?name="name" then you will not end up with this issue.

I hope this will help you.

srp
  • 619
  • 7
  • 18
-1

You might want to use @RestController instead of @Controller

@RestController from spring docs:

A convenience annotation that is itself annotated with @Controller and @ResponseBody.

When you use @Controller without @ResponseBody, the spring doesn't act like a pure rest end point.

YogendraR
  • 2,144
  • 12
  • 17