Spring MVC:无法连接到Rest服务

时间:2018-05-04 09:00:42

标签: java spring rest spring-mvc spring-boot

已经编写了spring controller Get方法,并尝试使用邮递员调用该方法但收到错误

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,因为我需要将呼叫重定向到网页。代码有什么问题吗?

请建议。

3 个答案:

答案 0 :(得分:1)

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

$finalimg = imagescale($newimg, 960, -1, IMG_BICUBIC);

没关系RestController,但需要添加请求映射

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

        SpringApplication.run(Application.class, args);

    }

您可能还需要在名为

的文件中添加视图文件的路径
  

application.properties

例如:

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

答案 1 :(得分:0)

我建议您查看您的网址和视图名称。

您尝试连接网址:连接到http://localhost:8081/yourPortal/shared?video0=14&video1=15

@Controller
public class SharedLinkController {

   @Autowired
   VideoService videoService;

   @RequestMapping("/shared")
   public ModelAndView getSharedVideo(ModelAndView mav,
        @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>();
        // ...

        mav.addObject("videos", videos);    
        mav.setViewName("index");

        return mav;
    }
}

答案 2 :(得分:0)

您需要为春季启动应用程序设置上下文, 尝试设置

server.context-path=/MyPortal

通过这种方式,您可以将应用程序的根上下文定义为

  

的http:本地主机:8081 / MyPortal /

相关问题