GVKun编程网logo

Java如何为Spring Boot应用程序配置端口(springboot jar指定端口)

27

如果您想了解Java如何为SpringBoot应用程序配置端口的相关知识,那么本文是一篇不可错过的文章,我们将对springbootjar指定端口进行全面详尽的解释,并且为您提供关于java–spri

如果您想了解Java如何为Spring Boot应用程序配置端口的相关知识,那么本文是一篇不可错过的文章,我们将对springboot jar指定端口进行全面详尽的解释,并且为您提供关于java – spring-boot应用程序无法启动、java – 在Weblogic中部署Spring Boot应用程序、java – 在没有Spring Boot应用程序的情况下使用Spring Boot Actuator、java – 如何从docker命令行设置参数来配置spring boot应用程序?的有价值的信息。

本文目录一览:

Java如何为Spring Boot应用程序配置端口(springboot jar指定端口)

Java如何为Spring Boot应用程序配置端口(springboot jar指定端口)

如何配置Spring Boot应用程序侦听的TCP / IP端口,因此它不使用默认端口8080。

答案1

小编典典

正如在所述的文档任一组server.port使用命令行选项来JVM中的系统属性-Dserver.port=8090或添加application.properties/src/main/resources/

server.port=8090

对于随机端口使用

server.port=0

java – spring-boot应用程序无法启动

java – spring-boot应用程序无法启动

我最近开始使用 spring-boot开发一个web应用程序,anf,按照官方网站上的指南,设法创建这两个文件:

的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>spring</groupId>
  <artifactId>app</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>app</name>
  <url>http://maven.apache.org</url>

  <properties>
    <start-class>com.spring.app.Application</start-class>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.1.8.RELEASE</version>
    </parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

</project>

Application.java

@Configuration
@EnableAutoConfiguration
@ComponentScan
public class Application {

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

        System.out.println("Let's inspect the beans provided by Spring Boot:");

        String[] beanNames = ctx.getBeanDeFinitionNames();
        Arrays.sort(beanNames);
        for (String beanName : beanNames) {
            System.out.println(beanName);
        }
    }

}

但是当我尝试用java -jar appname运行应用程序时,我得到错误:找不到主类:com.spring.app.Application.程序将退出,并在终端:线程“main”中的异常java.lang.NoClassDefFoundError:com / spring / app / Application.

我做错了什么?

解决方法

你的pom.xml中有2件事要做.

首先将start-class更改为您的应用程序类.
第二,将超酷的Spring Boot maven builder添加到你的pom中.

像这样的东西:

<?xml version="1.0" encoding="UTF-8"?>
<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.sample</groupId>
<artifactId>beanlist</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
    <start-class>com.sample.Application</start-class>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.1.8.RELEASE</version>
</parent>
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

</project

然后使用“mvn install”创建你的jar.你的代码运行得很好.

java – 在Weblogic中部署Spring Boot应用程序

java – 在Weblogic中部署Spring Boot应用程序

我在webLogic 12C中部署了一个 Spring引导应用程序.

10.4.4 403禁止
服务器了解该请求,但拒绝履行该请求.授权不会有帮助,请求不能重复.如果请求方法不是HEAD,并且服务器希望公开为什么请求尚未实现,则应该描述拒绝实体的原因.当服务器不希望明确地显示请求被拒绝的原因,或者没有其他响应适用时,通常使用此状态代码.

我想知道有人可以帮忙吗

解决方法

我审查了您的代码,并在此类代码中看到了一个问题:
https://github.com/purrox/Spring-example/blob/master/src/main/java/hello/Application.java

你正在做正确的(如SpringBoot文档中定义的),但它似乎有一个Weblogic12C的错误(或者也许是对标准的解释).似乎Weblogic12C搜索实现WebApplicationInitializer DIRECTLY的类.注意你的代码如何扩展SpringBootServletinitializer(它实现WebApplicationInitializer). Weblogic12C似乎并不喜欢它.因此,最简单的方法是使您的Application类实现WebApplicationInitializer.所以,改这行:

public class Application extends SpringBootServletinitializer {

到这个:

public class Application extends SpringBootServletinitializer implements WebApplicationInitializer {

注意:一旦修复了上面的内容,您将遇到另一个Weblogic12C部署问题:“java.lang.IllegalArgumentException:LoggerFactory不是Logback LoggerContext,但Logback位于类路径上”.要修复其他问题,请创建一个新文件src / main / webapp / WEB-INF / weblogic.xml并将其中的内容放入其中:

<?xml version="1.0" encoding="UTF-8"?>
    <wls:weblogic-web-app xmlns:wls="http://xmlns.oracle.com/weblogic/weblogic-web-app" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd http://xmlns.oracle.com/weblogic/weblogic-web-app http://xmlns.oracle.com/weblogic/weblogic-web-app/1.4/weblogic-web-app.xsd">
        <wls:weblogic-version>12.1.1</wls:weblogic-version>
        <wls:context-root>helloApp</wls:context-root>
        <wls:container-descriptor>
            <wls:prefer-application-packages>
                <wls:package-name>org.slf4j.*</wls:package-name>
            </wls:prefer-application-packages>
        </wls:container-descriptor>
    </wls:weblogic-web-app>

java – 在没有Spring Boot应用程序的情况下使用Spring Boot Actuator

java – 在没有Spring Boot应用程序的情况下使用Spring Boot Actuator

具有生产信息端点的 Spring Boot的Actuator库对任何服务器应用程序都非常有用.但问题是我找不到集成到传统Spring应用程序(不是Spring BOOT应用程序)的方法.

必须有一些方法来使用执行器的端点,但我无法将它们连接起来.

我有一个JavaConfig类,如下所示

@Configuration
@ComponentScan(basePackages = { "com.company.helper","org.springframework.boot" })
@EnableWebMvc
@Import({ DbConfig.class })

public class AppConfig extends WebMvcConfigurerAdapter {

}

但是此配置在部署期间会引发错误.

没有Spring Boot应用程序可以完成这种连线吗?

解决方法

我在此博客文章中添加了有关如何在非启动应用程序中添加spring boot执行器的信息

http://givenwhenthen.blogspot.com/2015/09/adding-spring-boot-actuator-to-non.html

在应用程序的build.gradle中,我添加了以下依赖项

compile('org.springframework.boot:spring-boot-actuator:1.2.5.RELEASE'){
    exclude group: 'org.springframework.boot',module:'spring-boot-starter-logging'}

在应用程序的Spring Config类中,我添加了以下内容:

import org.springframework.beans.factory.annotation.Autowired;  
 import org.springframework.boot.actuate.autoconfigure.EndpointAutoConfiguration;  
 import org.springframework.boot.actuate.endpoint.BeansEndpoint;  
 import org.springframework.boot.actuate.endpoint.HealthEndpoint;  
 import org.springframework.boot.actuate.endpoint.InfoEndpoint;  
 import org.springframework.boot.actuate.endpoint.RequestMappingEndpoint;  
 import org.springframework.boot.actuate.endpoint.mvc.EndpointHandlerMapping;  
 import org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter;  
 import org.springframework.boot.actuate.endpoint.mvc.HealthMvcEndpoint;  
 import org.springframework.boot.actuate.endpoint.mvc.MvcEndpoint;  

 @Configuration  
 @Import(EndpointAutoConfiguration.class)  
 public class MyAppSpringConfig {  

   @Bean  
   @Autowired  
   //Define the HandlerMapping similar to RequestHandlerMapping to expose the endpoint  
   public EndpointHandlerMapping endpointHandlerMapping(  
     Collection<? extends MvcEndpoint> endpoints  
   ){  
     return new EndpointHandlerMapping(endpoints);  
   }  

   @Bean  
   @Autowired  
   //define the HealthPoint endpoint  
   public HealthMvcEndpoint healthMvcEndpoint(HealthEndpoint delegate){  
     return new HealthMvcEndpoint(delegate,false);  
   }  

   @Bean  
   @Autowired  
   //define the Info endpoint  
   public EndpointMvcAdapter infoMvcEndPoint(InfoEndpoint delegate){  
      return new EndpointMvcAdapter(delegate);  
   }  

   @Bean  
   @Autowired  
   //define the beans endpoint  
   public EndpointMvcAdapter beansEndPoint(BeansEndpoint delegate){  
     return new EndpointMvcAdapter(delegate);  
   }  

   @Bean  
   @Autowired  
   //define the mappings endpoint  
   public EndpointMvcAdapter requestMappingEndPoint(  
     RequestMappingEndpoint delegate  
   ){  
     return new EndpointMvcAdapter(delegate);  
  }  
}

如果你想摆脱一个额外的依赖,那么请参阅博客帖子.

UPDATE

此外,您需要确保为RequestMappingHandlerAdapter定义了一个bean,如果您没有它,Servletdispatcher将无法为您的HealthMvcEndpoint的处理程序获取适配器.

如果你没有它只是将它添加到你的bean配置文件

xml配置:

<bean>
        <property name="messageConverters">
            <list>
                <ref bean="jsonConverter"/>
            </list>
        </property>
    </bean>

    <bean id="jsonConverter">
        <property name="supportedMediaTypes" value="application/json" />
        <property name="prettyPrint" value="true" />
    </bean>

java – 如何从docker命令行设置参数来配置spring boot应用程序?

java – 如何从docker命令行设置参数来配置spring boot应用程序?

我有一个带有yml的spring boot应用程序,可以在docker containter中配置它.
像这样的东西:

 spring:   application:
   name: micro-example
   config:
     uri: ${vcap.services.config-service.credentials.uri:http://xxx.xxx.xx.73:8888}

正如你所看到的,我的配置中有一个ip硬编码,这是一个坏主意,因为编译仅适用于服务器.
是否存在外部化ip的方法,或者从docker命令行或更好的想法设置它?

解决方法:

有很多不同的方法可以做到:

1)设置环境变量(在shell中使用export VCAP_SERVICES_CONfig-SERVICE_CREDENTIALS_URI =’http://example.com’,或在Dockerfile中使用ENV)

2)将其作为JVM参数传递(java -Dvcap.services.config-service.credentials.uri = http://example.com -jar app.jar)

3)将其作为命令行参数传递(java -jar app.jar –vcap.services.config-service.credentials.uri = http://example.com)

4)Spring Boot还从config / application.properties或application.properties读取值,这些值与可执行JAR文件位于同一目录中,因此可以提供此文件(可以使用VOLUME)并覆盖JAR中的设置

另见:http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html

我们今天的关于Java如何为Spring Boot应用程序配置端口springboot jar指定端口的分享已经告一段落,感谢您的关注,如果您想了解更多关于java – spring-boot应用程序无法启动、java – 在Weblogic中部署Spring Boot应用程序、java – 在没有Spring Boot应用程序的情况下使用Spring Boot Actuator、java – 如何从docker命令行设置参数来配置spring boot应用程序?的相关信息,请在本站查询。

本文标签: