试图在外部使用Tomcat 9上的REST控制器运行springboot war文件

时间:2019-02-14 09:39:10

标签: rest spring-boot tomcat

我一直在尝试部署springboot应用程序。我尝试了一个,可以将带有rest控制器的springboot部署到嵌入式tomcat中。现在,我再尝试将其打包为Web应用程序项目并部署到tomcat。我能够部署它,但是它不能正确解析以休息控制器路径并给出http 404错误。我正在使用gradle build进行此操作。

contextpath属性不起作用。

我的应用程序在http://localhost:8080/projectName/Welcome.jsp上启动。我需要从外部给它。单击我在jsp中的链接后,它会尝试移动到http:localhost:8080 / students。

这给出了错误-类型状态报告

消息/学生

描述原始服务器找不到目标资源的当前表示,或者不愿意透露该资源的存在。

我的gradle文件:

/*
 * This build file was generated by the Gradle 'init' task.
 *
 * This generated file contains a sample Java Library project to get you 
  started.
 * For more details take a look at the Java Libraries chapter in the 
 Gradle
 * user guide available at 
   https://docs.gradle.org/4.3/userguide/java_library_plugin.html
*/

 apply plugin: 'java'
 apply plugin: 'war'
 apply plugin: 'java-library'
 apply plugin: 'eclipse-wtp'
 apply plugin: 'org.springframework.boot'
 apply plugin: 'io.spring.dependency-management'

 repositories {
    mavenCentral()
  }

  buildscript {
    repositories {
    jcenter()
    mavenCentral()
  }

dependencies {
    classpath 'com.bmuschko:gradle-tomcat-plugin:2.5'
    classpath("org.springframework.boot:spring-boot-gradle- 
     plugin:2.0.5.RELEASE")

    //testImplementation 'junit:junit:4.12'
}
 }

   apply plugin: 'com.bmuschko.tomcat'

    sourceCompatibility = 1.8
     targetCompatibility = 1.8

   bootWar{
         mainClassName = 'org.sjsu.eds.student.main.StudentMain'
     }



    dependencies {
      // This dependency is exported to consumers, that is to say found 
  on their compile classpath.
   api 'org.apache.commons:commons-math3:3.6.1'

   // This dependency is used internally, and not exposed to consumers 
      on their own compile classpath.
      implementation 'com.google.guava:guava:23.0'

   // Use JUnit test framework
    testImplementation 'junit:junit:4.12'



    def tomcatVersion = '9.0.8'
    tomcat "org.apache.tomcat.embed:tomcat-embed-core:${tomcatVersion}",
       "org.apache.tomcat.embed:tomcat-embed-logging-juli:9.0.0.M6",
       "org.apache.tomcat.embed:tomcat-embed-jasper:${tomcatVersion}" 
       /*reference - https://github.com/bmuschko/gradle-tomcat- 
      plugin/blob/master/README.md*/

 compile("org.springframework.boot:spring-boot-starter-web")
  testCompile('org.springframework.boot:spring-boot-starter-test')

compile ("org.apache.httpcomponents:httpclient:4.5.7")
compile ("org.springframework:spring-webmvc:4.1.6.RELEASE")
}

  tomcat {
  httpProtocol = 'org.apache.coyote.http11.Http11Nio2Protocol'
   ajpProtocol  = 'org.apache.coyote.ajp.AjpNio2Protocol'
  }

我的春季主文件

 @SpringBootApplication
 public class StudentMain extends SpringBootServletInitializer{

@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder 
application) {
     return application.sources(StudentMain .class);
}

public static void main(String[] args) {
    // TODO Auto-generated method stub
    SpringApplication.run(StudentMain.class, args);
}

}

我的休息控制器很简单

@RestController
@RequestMapping(value="/students")
public class StudentController {

private StudentServiceImplWithoutDB studentService;

@Autowired
public StudentController(StudentServiceImplWithoutDB studentService) {
    this.studentService = studentService;
}


@GetMapping
public List<StudentVO> getAll(){

    List<StudentVO> studentVO= studentService.getAllStudents();
    return studentVO;

}
}

我需要为战争设置任何路径或属性吗?对于简单的Java应用程序,几乎相同的应用程序都具有嵌入的tomcat的魅力

1 个答案:

答案 0 :(得分:0)

首先,必须在将包类型更改为war之后,在gradle中提供tomcat作为ProvideRunTime,然后在主类中扩展SpringBootServletInitializer,将war文件上传到tomcat Web文件夹

按照这些步骤

首先添加gradle

providedRuntime 'org.springframework.boot:spring-boot-starter-tomcat'

更改程序包类型

apply plugin: "war"

在主类中扩展SpringBootServletInitializer类

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;

@SpringBootApplication
public class Application extends SpringBootServletInitializer {

  @Override
  protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
    return application.sources(Application.class);
  }

  public static void main(String[] args) {
    SpringApplication.run(Application.class, args);
  }
}

发动战争

gradle war