Home > Enterprise >  i have created my application as jar after changed into war file Now i am getting 404 error in my ap
i have created my application as jar after changed into war file Now i am getting 404 error in my ap

Time:08-25

@SpringBootApplication public class SpringApiApplication extends SpringBootServletInitializer implements WebApplicationInitializer { @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { return application.sources(SpringApiApplication.class); } public static void main(String[] args) throws Exception{ 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);
      
  }
    
    }

This is my pom.xml 4.0.0 org.springframework.boot spring-boot-starter-parent 2.7.3 com.springboot.app springboot-war-demo 0.0.1-SNAPSHOT *I created a war * war springboot-war-demo Spring Boot WAR Demo <java.version>17</java.version> org.springframework.boot spring-boot-starter-web

            <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>

CodePudding user response:

<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;

}

External tomcat application deployed logs

Access endpoint from Browser

CodePudding user response:

Thank you for your effort i created a new war package as existing package name and copied all application there while run in postman metion the war file name Eg-http://3.6.142.117:8080/warspring/api/student and tomcat ip address.

  • Related