首页 > 解决方案 > 在浏览器中以 localhost 的身份运行 Spring Boot 项目需要进行哪些更改?

问题描述

当我为 spring boot 项目运行命令时:

mvn 弹簧启动:运行

代码显示构建成功消息,并结束。

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 14.084 s
[INFO] Finished at: 2019-06-03T11:28:11+05:30
[INFO] Final Memory: 64M/899M
[INFO] ------------------------------------------------------------------------

但我想在http://localhost:8080上的浏览​​器上显示工作代码。应用程序立即停止,而不是启动 Web 服务器,项目结束。代码是否需要任何更改才能在 localhost 上运行?

这是我的 OMSApplication.java 类

@EnableScheduling
@SpringBootApplication(exclude=DataSourceAutoConfiguration.class)
@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class, DataSourceTransactionManagerAutoConfiguration.class,
        HibernateJpaAutoConfiguration.class, UserDetailsServiceAutoConfiguration.class})
@EnableEnvironmentJKS
public class OMSApplication extends SpringBootServletInitializer{

    private static final Logger log = LoggerFactory
            .getLogger(OMSApplication.class);


    public static void main(String[] args) {
        ApplicationContext applicationContext = SpringApplication.run(
                OMSApplication.class, args);
        log.info(" Application context created: {}" , applicationContext);
    }

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

}

这是 pom.xml 文件:

<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.example.mpms.services</groupId>
    <artifactId>oms-services</artifactId>
    <!-- <version>0.0.1-SNAPSHOT</version>-->
    <packaging>war</packaging>
    <name>oms-services</name>
    <description>oms services</description>

    <parent>
       <groupId>com.example.mpms</groupId>
       <artifactId>mpms-oms</artifactId>
       <version>0.0.1-SNAPSHOT</version>
    </parent>
    <build>
      <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
             <version>2.0.0.RELEASE</version>
            <configuration>    
                <skip>true</skip> 
                <!-- <systemPropertyVariables>
                    <APIE_PLATFORM>DEV</APIE_PLATFORM>
                    <APIE_REGION>US</APIE_REGION>
                    <APIE_ZONE>STL</APIE_ZONE>
                </systemPropertyVariables> -->
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>repackage</goal>
                    </goals>
                </execution>
            </executions> 
        </plugin>
        <plugin>
           <groupId>org.apache.maven.plugins</groupId>
           <artifactId>maven-site-plugin</artifactId>
           <version>${mvn-site-plugin-version}</version>
       </plugin>
      </plugins>
  </build>

</project>

我没有在这里包含所有依赖项,因为它们很多。

这是控制器类:


@RestController
public class OrderController {

    private static final Logger LOGGER = LoggerFactory.getLogger(OrderController.class); 

    @Autowired
    private KYCOrderService kycOrderService;

    /**
     * Health check URL for application via synapse.
     * 
     * */
    @GetMapping(value = "/health",produces = MediaType.TEXT_PLAIN_VALUE)
    @ResponseBody
    public ResponseEntity<String> healthCheck() {
        String output = String.format("Heartbeat :%s", "Cumulus- Order Management Service: I'm healthy!");
        LOGGER.info(output);
        return new ResponseEntity<>(output,HttpStatus.OK);
    }


    @PostMapping(path="/oms/order/kyc", produces = {MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_VALUE})
    @ApiOperation(value = "Place an Offline KYC Order", notes = "Order is placed in central oms", response = OfflineKYCOrderResponse.class, tags={ "OfflineKYCOrderResponse", })
    @ApiResponses(value = {
        @ApiResponse(code = 200, message = "OfflineKYCOrderResponse creation OK", response = OfflineKYCOrderResponse.class),
        @ApiResponse(code = 201, message = "Created", response = OfflineKYCOrderResponse.class),
        @ApiResponse(code = 400, message = "Bad Request", response = OfflineKYCOrderResponse.class),
        @ApiResponse(code = 401, message = "Unauthorized", response = OfflineKYCOrderResponse.class),
        @ApiResponse(code = 403, message = "Forbidden", response = OfflineKYCOrderResponse.class),
        @ApiResponse(code = 404, message = "Not Found", response = OfflineKYCOrderResponse.class)
    })
    public OfflineKYCOrderResponse placeOfflineKycOrder(@ApiParam(value = "Details of order to be placed" ,required=true)@RequestBody OfflineKYCOrderRequest offlineKYCOrder) {
        LOGGER.info("OfflineKYCOrderRequest: {}" , offlineKYCOrder);
        OfflineKYCOrderResponse errorResponse = offlineKYCOrder.validateInput();
        if (null != errorResponse) {
            LOGGER.info("OfflineKYCOrderResponse: {}" , errorResponse);
            return errorResponse;
        }
        ReasonCodes processStatusCode = ReasonCodes.OMS000;
        OfflineKYCOrderResponse offlineKYCOrderResponse = new OfflineKYCOrderResponse();
        try {
            KYCOrder createdKYCOrder = kycOrderService.saveOrder(new KYCOrder(offlineKYCOrder));
            offlineKYCOrderResponse.setOrderNumber(createdKYCOrder.getOrder().getOrderId());
        } catch (Exception e) {
            processStatusCode = ReasonCodes.OMS001;
            LOGGER.error("Failed to store order.", e);
        } finally {
            offlineKYCOrderResponse.setReasonCode(processStatusCode.toString());
            offlineKYCOrderResponse.setReasonDescription(processStatusCode.getDescription());
        }
        LOGGER.info("OfflineKYCOrderResponse: {}" , offlineKYCOrderResponse);
        return offlineKYCOrderResponse;
    }


}

任何人都可以帮助解决问题吗?

标签: postgresqlmavenspring-bootjpa

解决方案


您没有在 pom.xml 中包含 spring boot starter 包,从 pom.xml 文件中删除父依赖项并将其替换为以下依赖项

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.5.RELEASE</version>
</parent>

或者,您可以使用目标文件夹中的战争并使用 tomcat 或您选择的任何服务器进行部署。


推荐阅读