当前位置: 首页 > 知识库问答 >
问题:

我已将我的应用程序创建为jar后更改为war文件现在我在部署时在我的应用程序中收到404错误tomcat 8.5.9

穆博简
2023-03-14

@SpringBootApplication公共类 SpringApiApplication 扩展了 SpringBootServlet初始化器实现 Web应用程序初始化器 { @Override受保护的 SpringApplicationBuilder configure(SpringApplicationBuilder 应用程序) { 返回 application.sources(SpringApiApplication.class); } 公共静态 void main(字符串[] args) 抛出异常{ SpringApplication.run(SpringApiApplication.class, args); } }

*This is my controller page*

package com.javaproject.springapi.controller;
import java.util.List;

import org.springframework.http.HttpStatus;
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.javaproject.springapi.model.Student;
import com.javaproject.springapi.service.StudentService;
@RestController
@RequestMapping("/api" )
public class StudentController {
   
    private StudentService studentService;

    public StudentController(StudentService studentService) {
        super();
        this.studentService = studentService;
    }
    
    
    @PostMapping()
    public ResponseEntity< Student> addStudent(@RequestBody Student student){
        
     return new ResponseEntity<Student> (studentService.addStudent(student),HttpStatus.CREATED);
        
    }
    
    @GetMapping
    public List<Student> getStudents(){
        
        return studentService.getStudents();
        
    }
    
    
      * get student by id*
    @GetMapping("{id}")
    public ResponseEntity<Student> getStudentById(@PathVariable("id")long stuId){
        return new   ResponseEntity<Student>(studentService.getStudentById(stuId),HttpStatus.OK);
        }
    
    *Update Method*
    
  @PutMapping("{id}")
    
    public ResponseEntity<Student> updateStudent(@PathVariable("id")long id
                                                ,@RequestBody Student student){
    
    return new ResponseEntity<Student> (studentService.updateStudent(student, id),HttpStatus.OK);
    }
    
   * //Build Delete Method*
  @DeleteMapping("{id}")
  public ResponseEntity<String> deleteStudent(@PathVariable("id")long id){
                   studentService.deleteStudent(id); 
                   
      return new ResponseEntity<String>("Employee Deleted Successfully!.",HttpStatus.OK);
      
  }
    
    }

这是我的 pom.xml 4.0.0 组织.springframework.boot Spring启动器-父级 2.7.3 com.springboot.app Spring靴-战争-演示 0.0.1-快照 *我创建了一个战争 * 战争Spring靴-战争-战争-演示 Spring靴-战争-演示 Spring靴 战争演示

            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-tomcat</artifactId>
                <scope>provided</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    
    </project>

共有1个答案

凤扬
2023-03-14
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.7.3</version>
    <relativePath/>
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1</version>
<packaging>war</packaging>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
    <java.version>11</java.version>
</properties>
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.13.3</version>
    </dependency>
</dependencies>
<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
    <finalName>demo</finalName>
</build>
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;

@SpringBootApplication
public class DemoApplication extends SpringBootServletInitializer {
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

========

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@RestController
@RequestMapping("/api")
public class StudentController {

    @GetMapping("/students")
    public List<Student> getStudents() {
        Student.builder().id(1).name("test").build();
        return Stream.of(Student.builder().id(1).name("test").build()).collect(Collectors.toList());
    }

}

=======

import lombok.Builder;
import lombok.Data;

@Builder
@Data
public class Student {

    private long id;
    private String name;

}

部署的外部tomcat应用程序日志

从浏览器访问endpoint

 类似资料: