控制器到某些功能的映射在已部署的Spring Boot应用程序中不起作用

伊莱·约翰斯(Eli Johnes)

我已经在tomcat服务器中的vm中部署了spring boot应用程序。当我加载页面时,我只能看到静态部分,没有URI映射到控制器中的ajax函数似乎在这里起作用。但是在我的本地应用程序中也可以正常工作。谁能帮忙。我正在下面的REST控制器类的代码。

import org.springframework.data.rest.webmvc.RepositoryRestController;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
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;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.abc.xyz.util.DLCUtils;
import java.util.Date;
import java.util.List;


@RestController

public class HomeController {

    @RequestMapping("/home")
    public ModelAndView home(Model model) {
        int lastWeekInMonth = 0;
        lastWeekInMonth = DLCUtils.getLastWeekInMonth(2, 2016);
        System.out.println("last week"+lastWeekInMonth);
        model.addAttribute("lastweek", lastWeekInMonth);
        ModelAndView mav = new ModelAndView("/index");
        return mav;
    }
    @RequestMapping("/review")
    public ModelAndView reviewHours(Model model) {
        ModelAndView mav = new ModelAndView("/review");
        return mav;
    }
    @RequestMapping("/manage")
    public ModelAndView manageHours(Model model) {

        ModelAndView mav = new ModelAndView("/manage");
        return mav;
    }
    @RequestMapping("/addnewemployee")
    public ModelAndView addnewemployee(Model model) {
        ModelAndView mav = new ModelAndView("/addemployee");
        return mav;

    }

    @RequestMapping("/populateWeek")
    @ResponseBody
    public Integer lastweekMethod() {
        int lastWeekInMonth = 0;
        lastWeekInMonth = DLCUtils.getLastWeekInMonth(2, 2016);
        System.out.println("last week in lastweek"+lastWeekInMonth);
        return lastWeekInMonth;
    }

    @RequestMapping("/getCurrentWeek")
    @ResponseBody
    public Integer getCurrentweek() {
        int weekOfMonth = 0;
        weekOfMonth = DLCUtils.getWeekOfTheMonth();
        //System.out.println("last week in lastweek"+lastWeekInMonth);
        return weekOfMonth;
    }

    @RequestMapping(value="/getCurrentMonth",method=RequestMethod.GET)
    @ResponseBody
    public int getCurrentMonth(@RequestParam("monthname") String month) {
        int monthnumber = 0;
        monthnumber = DLCUtils.getMonthNumber(month.toUpperCase());
        //System.out.println("last week in lastweek"+lastWeekInMonth);
        return monthnumber;
    }



    @RequestMapping(value="/getRangeOfDate",method=RequestMethod.GET)
    @ResponseBody
    public String getRangeOfDate(@RequestParam("week") int week,@RequestParam("month") int month,@RequestParam("year") int year) {
        System.out.println("week val"+week);
        List<Date> dateRange=null;
        StringBuilder submitList = new StringBuilder("");
        Date fd=null,ld=null;
        dateRange = DLCUtils.getAllDays(week, month, year);
        for (int i = 0; i < dateRange.size(); i++) {
            if (i == 0) {
                fd = dateRange.get(0);
            }
            if (i == dateRange.size() - 1) {
                ld = dateRange.get(i);
            }

        }
        String firstdate = null;
        String lastdate = null;
        int fdate = 0;
        int ldate = 0;
        firstdate = fd.toString();
        lastdate = ld.toString();
        String firstdate1 = firstdate.substring(8, 10);
        String lastdate1 = lastdate.substring(8, 10);
        fdate = Integer.parseInt(firstdate1);
        ldate = Integer.parseInt(lastdate1);
        String monthName=DLCUtils.getMonthName(month);
        submitList.append(monthName);submitList.append("+");submitList.append(fdate);submitList.append("+");submitList.append(ldate);submitList.append("+");submitList.append(DLCUtils.getCurrentYear());
        return submitList.toString();
    }
    @RequestMapping(value="/getLastDate",method=RequestMethod.GET)
    @ResponseBody
    public String getLastDate(@RequestParam("month") int month,@RequestParam("year") int year){

        return  (DLCUtils.getEndDateOfTheMonth(month, year).toString());

    }
    @RequestMapping(value="/getAlldaysInweek",method=RequestMethod.GET)
    @ResponseBody
    public String getListDatesInAWeek(@RequestParam("week") int week,@RequestParam("month") int month,@RequestParam("year") int year){

        List<Date> lstDates = null;
        lstDates = DLCUtils.getAllDays(week, month, year);
        String dayOfWeek=null;
        StringBuilder submitList = new StringBuilder("");
        String formattedDate = null;
        String pattern = "yyyy/MM/dd";
        for (Date date : lstDates) {
        formattedDate = DLCUtils.getDateInPattern(date, pattern);
        dayOfWeek=DLCUtils.getDayOfTheWeek(date);
        submitList.append(dayOfWeek);submitList.append(",");submitList.append(formattedDate);submitList.append("+");
        }
        return submitList.toString();
    }
    @RequestMapping(value="/populatedetails",method=RequestMethod.GET)
    @ResponseBody
    public String populatedetails(@RequestParam("name") String name,@RequestParam("Id") String id,@RequestParam("Email") String email){

        StringBuilder submitList = new StringBuilder("");
        String place="Bangalore";String joiningdate="2014-01-01";String enddate="9999-12-31";String rights="Employee";String status="Active";String dept="EBU";String manager="David";
        submitList.append("Name"+"+"+name+","+"GlobalId"+"+"+id+","+"Email"+"+"+email+","+"Location"+"+"+place+","+"Joining Date"+"+"+joiningdate+","+"Resignation Date"+"+"+enddate+","+"Rights"+"+"+rights+",");


        submitList.append("Status"+"+"+status+","+"Division"+"+"+dept+","+"Manager"+"+"+manager+","+"Projects"+"+");
        submitList.append("SPT"+".");submitList.append("SPM"+".");submitList.append("DLC"+".");submitList.append("TEST1"+".");submitList.append("TEST2"+".");submitList.append("TEST3"+".");
        return submitList.toString();
    }   
}

除此之外,仅在控制台中显示,不会返回那些返回模型和视图的函数,以及所有其他映射到从angular js ajax调用中调用的函数的函数。

angular.js:8611 GET http://172.16.77.83:8083/getCurrentWeek 404 (Not Found)

其他呼叫也是如此。因此,我的html页面中的数据未正确填充。相同的应用程序在我的本地计算机上可以正常工作。仅在此外部虚拟机中,它显示此问题。

添加js文件的ajax调用代码。

function getCurrentWeek(){
        console.log("in ajax");

        $http({
            method : "GET",
            url : "/getCurrentWeek"
        }).then(function mySucces(response) {
            $scope.currentWeek=response.data;
              var j=parseInt($scope.currentWeek)-1;


              $('table.ui-datepicker-calendar tr:eq('+$scope.currentWeek+')').css({"border":"3px solid #e1e1e1"});
              var weekVal=$('#weekvalues a')[j];
             weekVal.style.border="3px solid #50a8e5";
              weekVal.style.paddingRight="2px";
              weekVal.style.paddingLeft="2px";
              weekVal.style.paddingTop="1px";

        }, function myError(response) {
            //$scope.dateRange = response.statusText;
        });

    }

谢谢,Poorna。

g00glen00b

在我看来,当您在Tomcat容器(VM内)上进行部署时,您正在使用不同的上下文路径。如果您使用本地运行该应用程序mvn spring-boot:run,或者在不设置该server.contextPath属性的情况下启动了JAR ,则可能在嵌入式容器内的根上下文上运行了该应用程序

该URLhttp://172.16.77.83:8083/getCurrentWeek在该根上下文上运行,而在Tomcat上,您可能必须转到http://172.16.77.83:8083/MyApplicationNam/getCurrentWeek

要解决此问题,有一些解决方案:

1.在根上下文上部署应用程序

  1. ROOT.war在将其部署到Tomcat之前,将WAR重命名为
  2. 在中更改上下文路径 server.xml

检查此答案以获取有关它的更多信息:在Tomcat的根目录中部署我的应用程序


2.使用相对URL(可能是最佳解决方案)

通过使用相对URL,您将始终基于HTML页面相对工作。如果HTML页面在同一应用程序内,则上下文路径也将应用于HTML页面,因此相对于HTML页面而言,REST API应该始终保持不变。

$http({
    method : "GET",
    url : "./getCurrentWeek"
})

有关此主题的更多信息,请检查以下答案:绝对URL与相对URL

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

CommonsRequestLoggingFilter在Spring Boot应用程序中不起作用

Spring-boot 应用程序:端口映射在 docker 中不起作用

onetoone映射在我的Spring Boot Rest应用程序和json mysql中不起作用

简单的Spring Boot应用程序不起作用

映射的键在某些应用程序中不起作用

MapStruct实现在Spring Boot Web应用程序中不起作用

Spring Boot 应用程序。批处理在 JpaRepository.saveAll 方法中不起作用

带有JSTL的JSP在Spring Boot应用程序中的tomcat 8上不起作用

带有@Scheduled批注的方法在Spring Boot应用程序中不起作用

注销在Spring Boot应用程序中不起作用(不支持POST方法)

ServiceLoader在打包的Spring Boot应用程序中不起作用

为什么AOP在我的Spring Boot应用程序中不起作用?

在Spring Boot应用程序中配置cors。Bean CorsConfigurationSource不起作用

为什么@PostConstruct 在 Spring Boot 应用程序中不起作用?

弹性搜索在Spring Boot应用程序中不起作用

Flask应用程序在某些目录中不起作用

注释 @ActiveProfile 在 Spring 应用程序中不起作用

使用Docker传递环境变量到Spring Boot应用程序不起作用

Spring Boot应用程序的docker secret在docker swarm模式/ run / secrets下不起作用

使用“以Spring Boot应用程序身份运行”启动Apllication时,JUnitCore运行不起作用

将Spring Boot Web应用程序部署到JBoss-webjars-locator似乎不起作用

ALLOWED_HOSTS在部署到Elastic Beanstalk的Django应用程序中不起作用

CentO中的AWS中的node.js应用程序部署不起作用

Heroku 应用程序不起作用(部署问题?)

从 Qt 到 UWP 应用程序的 PostMessage 不起作用

Angular 5应用程序部署-组件URL路由在S3中不起作用

angular 和 nodejs 部署的应用程序中的路由不起作用

FBSDK共享在本机应用程序中不起作用。仅在某些android设备上

鼠标向上滚动在某些应用程序中不起作用