我无法打开Spring Boot网站吗?

时间:2018-09-26 19:18:06

标签: java rest spring-boot web

在以Java应用程序运行之前,我的项目具有MySql,Spring-boot,REST服务。 我想部署Web应用程序,但系统报告错误:Whitelabel错误页面...

enter image description here

控制器:

@RestController
@RequestMapping("/autoeshop")
public class EmployeeController {

    @Autowired
    EmployeeDAO employeeDAO;

    /* to save an employee*/
    @PostMapping("/employees")
    public Employee saveEmployee(@Valid @RequestBody Employee emp) {
        return employeeDAO.save(emp);
    }

    /*get an employee by id*/
    @GetMapping("/employees/{id}")
    public ResponseEntity<Employee> getEmployee(@PathVariable(value="id") Long empId){
        Employee emp=employeeDAO.get(empId);

        if(emp==null)
            return ResponseEntity.notFound().build();
        return ResponseEntity.ok().body(emp);
    }   

    /*get all employees*/
    @GetMapping("/employees")
    public List<Employee> getAllEmployees(){
        return employeeDAO.getAll();
    }

    /*update an employee*/
    @PutMapping("/employees")
    public void updateEmployee(@PathVariable(value="id") Long empId, @Valid @RequestBody Employee empDetails) {

        Employee emp = employeeDAO.get(empId);
        //if(emp==null)
        //  return ResponseEntity.notFound().build();

        emp.setName(empDetails.getName());
        emp.setDesignation(empDetails.getDesignation());
        emp.setExpertise(empDetails.getExpertise());

        //return ResponseEntity.ok().body((Employee)employeeDAO.update(emp));
        employeeDAO.update(emp);
    }

    /*Delete an employee*/
    @DeleteMapping("/employees/{id}")
    public ResponseEntity<Employee> deleteEmployee(@PathVariable(value="id") Long empId){
        //Employee emp=employeeDAO.get(empId);

        if(employeeDAO.get(empId)==null)
            return ResponseEntity.notFound().build();
        employeeDAO.delete(empId);
        return ResponseEntity.ok().build();
    }
}

application.properties:

  

Spring DATASOURCE(数据源自动配置和数据源属性)

spring.datasource.url = jdbc:mysql://localhost:3306/autoeshop
spring.datasource.username = root
spring.datasource.password =



## Hibernate Properties
# The SQL dialect makes Hibernate generate better SQL for the chosen database
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect

# Hibernate ddl auto (create, create-drop, validate, update)
spring.jpa.hibernate.ddl-auto = update

请告诉我如何帮助我部署Web应用程序?谢谢

2 个答案:

答案 0 :(得分:1)

该应用程序实际已部署:

在春季启动中,仅当应用程序成功启动(应用程序上下文启动)时,http端口才会暴露(在您的情况下为8081)

但是,应用程序中没有注册的/autoeshop/employee映射。 您可以检查spring应用程序的启动日志,它记录所有已注册的映射。

现在,如果没有映射,则默认行为是返回此错误页面+ http 404(未找到)状态,这就是您所看到的。

因此,最重要的是,您应该检查控制器映射,它可能会归结为以下其中一项:

  • 控制器为您尝试检查的注释指定了不同的映射
  • Spring Boot找不到您的控制器(有很多可能的原因)

更新:我已经看到了您的代码,确实没有GET请求与 /autoeshop/employee

相反,您有一个到/autoeshop/employees(多个)的映射

 /*get all employees*/
@GetMapping("/employees")
public List<Employee> getAllEmployees(){
    return employeeDAO.getAll();
}

答案 1 :(得分:0)

哦,我忘了输入密码。

控制器:

package com.autoparts.autoeshop.controller;

import java.util.List;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.autoparts.autoeshop.dao.EmployeeDAO;
import com.autoparts.autoeshop.model.Employee;

@RestController
@RequestMapping("/autoeshop")
public class EmployeeController {

    @Autowired
    EmployeeDAO employeeDAO;

    /* to save an employee*/
    @PostMapping("/employees")
    public Employee saveEmployee(@Valid @RequestBody Employee emp) {
        return employeeDAO.save(emp);
    }

    /*get an employee by id*/
    @GetMapping("/employees/{id}")
    public ResponseEntity<Employee> getEmployee(@PathVariable(value="id") Long empId){
        Employee emp=employeeDAO.get(empId);

        if(emp==null)
            return ResponseEntity.notFound().build();
        return ResponseEntity.ok().body(emp);
    }   

    /*get all employees*/
    @GetMapping("/employees")
    public List<Employee> getAllEmployees(){
        return employeeDAO.getAll();
    }

    /*update an employee*/
    @PutMapping("/employees")
    public void updateEmployee(@PathVariable(value="id") Long empId, @Valid @RequestBody Employee empDetails) {

        Employee emp = employeeDAO.get(empId);
        //if(emp==null)
        //  return ResponseEntity.notFound().build();

        emp.setName(empDetails.getName());
        emp.setDesignation(empDetails.getDesignation());
        emp.setExpertise(empDetails.getExpertise());

        //return ResponseEntity.ok().body((Employee)employeeDAO.update(emp));
        employeeDAO.update(emp);
    }

    /*Delete an employee*/
    @DeleteMapping("/employees/{id}")
    public ResponseEntity<Employee> deleteEmployee(@PathVariable(value="id") Long empId){
        //Employee emp=employeeDAO.get(empId);

        if(employeeDAO.get(empId)==null)
            return ResponseEntity.notFound().build();
        employeeDAO.delete(empId);
        return ResponseEntity.ok().build();
    }
}

application.properties:

## Spring DATASOURCE (DataSourceAutoConfiguration & DataSourceProperties)
spring.datasource.url = jdbc:mysql://localhost:3306/autoeshop
spring.datasource.username = root
spring.datasource.password = 
server.port=8081



## Hibernate Properties
# The SQL dialect makes Hibernate generate better SQL for the chosen database
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect

# Hibernate ddl auto (create, create-drop, validate, update)
spring.jpa.hibernate.ddl-auto = update