Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
242 views
in Technique[技术] by (71.8m points)

Replacing @Controller with @RestController in Spring Boot does not show intended view

While running tests against this controller, I get the Circular View Path error. Replacing @Controller with @RestController fixes this error however now instead of the view showing in the browser I get the actual returned String in the view.

Example would be, when I replace @Controller below with @RestController and make a GET request mapping to "/ListA", the controller method returns the String ListA in the browser as in all you see printed in the browser are the letters ListA and the view with all my inputs, buttons, tables do not show.

What am I missing here?

 @RestController
 @RequestMapping
 public class WebController {
    
      @GetMapping("/ListA") 
      public String viewListAController() {
          
          return "ListA";

      }

      @GetMapping("/ListB") 
      public String viewListBController() {
          
          return "ListB";

      }
 }  
question from:https://stackoverflow.com/questions/65598795/replacing-controller-with-restcontroller-in-spring-boot-does-not-show-intended

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

@RestController simplifies the creation of RESTful web services. It's a convenience annotation that combines @Controller and @ResponseBody.

if you use it it will convert the response into a JSON object format. That's why you are getting a string as text in the browser, not the view.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...