我正在开发与远程主机通信的简单Spring Web应用程序,我想在公司代理后面进行本地测试.我使用"Spring Boot"gradle插件,问题是如何为JVM指定代理设置?
我尝试了几种方法:
gradle -Dhttp.proxyHost=X.X.X.X -Dhttp.proxyPort=8080 bootRunexport JAVA_OPTS="-Dhttp.proxyHost=X.X.X.X -Dhttp.proxyPort=8080"export GRADLE_OPTS="-Dhttp.proxyHost=X.X.X.X -Dhttp.proxyPort=8080"但似乎没有一个工作 - "NoRouteToHostException"抛出"网络"代码.另外,我添加了一些额外的代码来调试JVM启动参数:
RuntimeMXBean runtimeMxBean = ManagementFactory.getRuntimeMXBean();
List<String> arguments = runtimeMxBean.getInputArguments();
for (String arg: arguments) System.out.println(arg);
Run Code Online (Sandbox Code Playgroud)
并且只打印了一个参数:" - Dfile.encoding = UTF-8".
如果我在代码中设置系统属性:
System.setProperty("http.proxyHost", "X.X.X.X");
System.setProperty("http.proxyPort", "8080");
Run Code Online (Sandbox Code Playgroud)
一切正常!
我正在尝试在Spring Boot网站上调整REST控制器示例.不幸的是,当我尝试访问localhost:8080/itemURL 时,我遇到以下错误.
{
"timestamp": 1436442596410,
"status": 404,
"error": "Not Found",
"message": "No message available",
"path": "/item"
}
Run Code Online (Sandbox Code Playgroud)
POM:
<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>SpringBootTest</groupId>
<artifactId>SpringBootTest</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<javaVersion>1.8</javaVersion>
<mainClassPackage>com.nice.application</mainClassPackage>
<mainClass>${mainClassPackage}.InventoryApp</mainClass>
</properties>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>${javaVersion}</source>
<target>${javaVersion}</target>
</configuration>
</plugin>
<!-- Makes the Spring Boot app executable for a jar file. The additional configuration is needed for the cmd: mvn spring-boot:repackage
OR mvn spring-boot:run -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>${mainClass}</mainClass>
<layout>ZIP</layout>
</configuration>
<executions>
<execution>
<goals> …Run Code Online (Sandbox Code Playgroud) 我有一个带有以下内容的Spring Boot应用程序application.yml- 基本上从这里开始:
info:
build:
artifact: ${project.artifactId}
name: ${project.name}
description: ${project.description}
version: ${project.version}
Run Code Online (Sandbox Code Playgroud)
我可以注入特定的值,例如
@Value("${info.build.artifact}") String value
Run Code Online (Sandbox Code Playgroud)
但是,我想注入整个地图,例如:
@Value("${info}") Map<String, Object> info
Run Code Online (Sandbox Code Playgroud)
是(或类似的)可能吗?显然,我可以直接加载yaml,但是想知道Spring是否已经支持了某些东西.
在启动期间运行spring boot应用程序时,我遇到异常:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'testController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.springframework.web.client.RestTemplate com.micro.test.controller.TestController.restTemplate; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.web.client.RestTemplate] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
Run Code Online (Sandbox Code Playgroud)
我在TestController中自动装配RestTemplate.我正在使用Maven进行依赖管理.
TestMicroServiceApplication.java
package com.micro.test;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class TestMicroServiceApplication {
public static void main(String[] args) {
SpringApplication.run(TestMicroServiceApplication.class, args);
}
}
Run Code Online (Sandbox Code Playgroud)
TestController.java
package com.micro.test.controller; …Run Code Online (Sandbox Code Playgroud) 我正在尝试将 swagger-ui (OpenAPI 3.0) 添加到 Spring Boot v3 应用程序中。
我已经添加了 openapi-ui maven 依赖项,它应该按照文档工作。
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-ui</artifactId>
<version>1.6.11</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
但显然,它仍然不起作用,并且 localhost:8080/swagger-ui.html 返回 404 错误。
我缺少什么?
我有一个简单的Spring Boot应用程序,它从JMS队列获取消息并将一些数据保存到日志文件中,但不需要Web服务器.有没有办法在没有Web服务器的情况下启动Spring Boot?
我使用Spring Boot作为一个简单的REST API,并希望在出现故障时返回正确的HTTP状态代码.
@RequestMapping(value="/rawdata/", method = RequestMethod.PUT)
@ResponseBody
@ResponseStatus( HttpStatus.OK )
public RestModel create(@RequestBody String data) {
// code ommitted..
// how do i return a correct status code if something fails?
}
Run Code Online (Sandbox Code Playgroud)
作为Spring和Spring Boot的新手,基本的问题是,当某些东西正常或失败时,我如何返回不同的状态代码?
我尝试在我的application.yml configration中使用env变量,如:
spring:
main:
show_banner: false
---
spring:
profiles: production
server:
address: $OPENSHIFT_DIY_IP
port: $OPENSHIFT_DIY_PORT
Run Code Online (Sandbox Code Playgroud)
但是env变量没有解决.我必须提供不同的符号吗?
在Rails中,您可以使用<%= ENV ['FOOVAR']%>
唯一的选择是运行应用程序,如:
java -jar my.jar --server.address=$OPENSHIFT_DIY_IP --server.port=$OPENSHIFT_DIY_PORT
Run Code Online (Sandbox Code Playgroud) 在maven项目中,我尝试使用maven资源过滤替换一些令牌,但它不起作用.我有一些其他项目可行,但在这个单项目中不起作用,不确定有什么问题.
属性文件位于/src/main/resources/my.properties中
我尝试了不同的maven命令如下,但不起作用.
mvn clean install
mvn clean install resources:resources
Run Code Online (Sandbox Code Playgroud)
my.properties
### Spring boot properties
jdbc.url=${jdbc.url}
ldap.domain=${ldap_domain}
ldap.url=${ldap_url}
Run Code Online (Sandbox Code Playgroud)
的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.jai</groupId>
<artifactId>client</artifactId>
<version>0.0.6-SNAPSHOT</version>
<name>client</name>
<description>client web application</description>
<packaging>war</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.2.RELEASE</version>
<relativePath />
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<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>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-ldap</artifactId>
</dependency>
</dependencies>
<build>
<finalName>client</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<configuration> …Run Code Online (Sandbox Code Playgroud) java空间中似乎有一种趋势,即不再以war文件(或ear文件)的形式将java Web应用程序部署到java servlet容器(或应用程序服务器),而是将应用程序打包为可执行jar.像jetty这样的嵌入式servlet/HTTP服务器.我的意思是,更新的框架影响新应用程序的开发和部署方式,而不是如何将应用程序交付给最终用户(因为,例如,我知道Jenkins使用嵌入式容器,非常容易抓住和去).采用可执行jar选项的框架示例: Dropwizard,Spring Boot和Play (它不是在servlet容器上运行,而是嵌入了HTTP服务器).
我的问题是,来自我们已经将我们(直到这一点,主要是Struts2)应用程序部署到单个tomcat应用程序服务器的环境,如果我们计划使用嵌入式容器方法,需要做出哪些更改,最佳实践或注意事项?目前,我们在单个tomcat服务器上运行大约10个自行开发的应用程序,对于这些小型应用程序,共享资源并在一台服务器上进行管理的能力很不错.我们的应用程序无意分发给最终用户以在其环境中运行.但是,如果我们决定利用更新的Java框架,那么这种方法会改变吗?越来越多地使用云部署(例如Heroku)刺激了对可执行jar的转变?
如果您有使用Play风格的部署管理多个应用程序而不是单个应用程序服务器上的传统war文件部署的经验,请分享您的见解.
spring-boot ×10
java ×8
spring ×5
maven ×2
rest ×2
dropwizard ×1
gradle ×1
resttemplate ×1
swagger-ui ×1
war ×1
yaml ×1