首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在变成war文件后,我已经将我的应用程序创建为jar,现在我在应用程序中部署tomcat 8.5.9时得到404错误

在变成war文件后,我已经将我的应用程序创建为jar,现在我在应用程序中部署tomcat 8.5.9时得到404错误
EN

Stack Overflow用户
提问于 2022-08-24 05:32:45
回答 1查看 59关注 0票数 1

@SpringBootApplication公共类SpringApiApplication扩展SpringBootServletInitializer实现WebApplicationInitializer {@覆盖受保护的SpringApplicationBuilder配置(SpringApplicationBuilder应用程序){返回SpringApiApplication}公共静态空洞main(String[] args)抛出异常{ SpringApplication.run(SpringApiApplication.class,args);}

代码语言:javascript
复制
*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 org.springframework.boot Spring -启动器-家长2.7.3 com.springboot.app springboot- war -演示0.0.1-快照*我创建了一个WAR * war -WAR-演示springboot演示>17 org.springframework.boot spring-starter

代码语言:javascript
复制
            <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>
EN

回答 1

Stack Overflow用户

发布于 2022-08-24 05:54:34

代码语言:javascript
复制
<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>

===========

代码语言:javascript
复制
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);
    }
}

========

代码语言:javascript
复制
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());
    }

}

=======

代码语言:javascript
复制
import lombok.Builder;
import lombok.Data;

@Builder
@Data
public class Student {

    private long id;
    private String name;

}

外部tomcat应用程序部署日志

从浏览器访问端点

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73467869

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档