首页 > 解决方案 > Spring MVC:无法连接到 Rest 服务

问题描述

编写了一个spring controllerGet 方法并尝试用邮递员调用它但出现错误

Could not get any response
There was an error connecting to https://localhost:8081/MyPortal/shared?video0=14&video1=15.

即使我尝试调试,调用也不会转到控制器方法。不知道发生了什么。

这是控制器代码:

@Controller
public class SharedLinkController {

@Autowired
VideoService videoService;

@RequestMapping("/shared")
public  ModelAndView getSharedVideo(HttpServletRequest request, HttpServletResponse response,
        @RequestParam(value="video0", required=false) Long video0,
        @RequestParam(value="video1", required=false) Long video1,
        @RequestParam(value="video2", required=false) Long video2,
        @RequestParam(value="video3", required=false) Long video3){

    List<Lab> videos = new ArrayList<Lab>();

        // some processing with video0, video1 etc....

    ModelAndView model = new ModelAndView("index");
    model.addObject("videos", videos);
    return model;

}

}

这就是我尝试调用 API 的方式:

https://localhost:8081/MyPortal/shared?video0=14&video1=15

我不想让它成为一个RestController,因为我需要将呼叫重定向到网页。代码有什么问题吗?

请建议。

标签: javaspringrestspring-mvcspring-boot

解决方案


您是否有一个扫描基本包并将您的控制器注册为 bean 的类,如下所示?

@SpringBootApplication(scanBasePackages= "se.yourpackage.src")
@EnableAutoConfiguration
public class Application {
public static void main(String[] args) {

        SpringApplication.run(Application.class, args);

    }

没关系 RestController,但你需要添加 requestmapping

@RequestMapping("/MyPortal")
public class SharedLinkController {

您可能还需要在名为的文件中添加视图文件的路径

应用程序属性

例如:

spring.mvc.view.prefix=/WEB-INF/view/
spring.mvc.view.suffix=.jsp

推荐阅读