小编FrV*_*aBe的帖子

覆盖Junit Test中的默认Spring-Boot application.properties设置

我有一个Spring-Boot应用程序,其中默认属性设置在application.properties类路径中的文件中(src/main/resources/application.properties).

我想在我的JUnit测试中覆盖一些默认设置,其中包含在test.properties文件中声明的属性(src/test/resources/test.properties)

我通常会为我的Junit测试提供专用的Config类,例如

package foo.bar.test;

import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

@Configuration
@Import(CoreConfig.class)
@EnableAutoConfiguration
public class TestConfig {

}
Run Code Online (Sandbox Code Playgroud)

我首先想到@PropertySource("classpath:test.properties")在TestConfig类中使用可以解决这个问题,但这些属性不会覆盖application.properties设置(请参阅Spring-Boot参考文档 - 23.外部化配置).

然后我尝试-Dspring.config.location=classpath:test.properties在调用测试时使用.这很成功 - 但我不想为每次测试执行设置此系统属性.因此我把它放在代码中

@Configuration
@Import(CoreConfig.class)
@EnableAutoConfiguration
public class TestConfig {

  static {
    System.setProperty("spring.config.location", "classpath:test.properties");
  }

}
Run Code Online (Sandbox Code Playgroud)

不幸的是,再次没有成功.

必须有一个关于如何application.properties在JUnit测试中覆盖设置的简单解决方案test.properties,我必须忽略它.

java unit-testing spring-boot

174
推荐指数
7
解决办法
18万
查看次数

需要实现Virgil Dobjanschi REST实现模式的示例Android REST Client项目

我想在Android手机上构建一个REST客户端.

REST服务器公开了几个资源,例如(GET)

http://foo.bar/customer      List of all customer
http://foo.bar/customer/4711    The customer with id 4711
http://foo.bar/customer/vip     List of all VIP customer

http://foo.bar/company           List of all companys
http://foo.bar/company/4711     The company with the ID 4711
http://foo.bar/company/vip      List of all VIP companys
Run Code Online (Sandbox Code Playgroud)

我(想)我知道如何与REST服务器通信并获取我需要的信息.我将使用这样的API实现REST Client类

public List<Customer> getCustomers();
public Customer getCustomer(final String id);
public List<Customer> getVipCustomer();

public List<Company> getCompanies();
public Customer getCompany(final String id);
public List<Customer> getVipCompanies();
Run Code Online (Sandbox Code Playgroud)

参考了Virgil Dobjanschi的" 开发Android REST客户端应用程序 " 演示文稿,我了解到在Activity的Worker Thread中处理REST请求并不是一个好主意.相反,我应该使用服务 API.

我喜欢将Singleton ServiceHelper绑定到(本地)服务的想法,但我担心我不理解服务概念是否正确.

目前我不明白如何将REST调用结果(在服务中完成异步)报告回调用者Activity.我也想知道我是否需要一个服务来处理所有REST请求(具有不同的返回类型),或者我是否需要为每个REST请求提供专用服务.

可能我还有很多其他的理解问题,所以对我来说最好的事情就是满足我需求的示例应用程序.我的用例并不罕见,我希望那里有示例应用程序.

你能让我知道吗

任何其他建议指出我在正确的实现方向也是有帮助的(Android API-Demo与我的用例不匹配).

提前致谢. …

java rest android design-patterns

82
推荐指数
4
解决办法
4万
查看次数

pathParameters文档异常(找不到urlTemplate)

当使用pathParameters记录URI路径参数,如下面

@Test
public void documentGetRouteById() throws Exception {
    this.mockMvc.perform(get("/route/{id}", "FooBar")).andExpect(status().isOk())
            .andDo(document("api-getRouteById",
                    pathParameters(parameterWithName("id").description("die Routen ID"))));
}
Run Code Online (Sandbox Code Playgroud)

我得到以下激励

java.lang.IllegalArgumentException: urlTemplate not found. Did you use RestDocumentationRequestBuilders to build the request?
    at org.springframework.util.Assert.notNull(Assert.java:112)
    at org.springframework.restdocs.request.PathParametersSnippet.extractUrlTemplate(PathParametersSnippet.java:95)
    at org.springframework.restdocs.request.PathParametersSnippet.extractActualParameters(PathParametersSnippet.java:82)
    at org.springframework.restdocs.request.AbstractParametersSnippet.verifyParameterDescriptors(AbstractParametersSnippet.java:77)
    at org.springframework.restdocs.request.AbstractParametersSnippet.createModel(AbstractParametersSnippet.java:65)
    at org.springframework.restdocs.request.PathParametersSnippet.createModel(PathParametersSnippet.java:67)
    at org.springframework.restdocs.snippet.TemplatedSnippet.document(TemplatedSnippet.java:64)
    at org.springframework.restdocs.mockmvc.RestDocumentationResultHandler.handle(RestDocumentationResultHandler.java:101)
    at org.springframework.test.web.servlet.MockMvc$1.andDo(MockMvc.java:158)
Run Code Online (Sandbox Code Playgroud)

我敢肯定,我做了测试设置类似解释在这里.

我可能做错了什么?

(Spring REST docs版本是1.0.0.BUILD-SNAPSHOT)

java spring-restdocs

24
推荐指数
1
解决办法
3985
查看次数

保留mod_proxy重定向上的原始请求URL

我在Servlet容器(端口8080)上运行WebApplication,该环境可以从Internet(外部)和公司内部(内部)访问,例如

http://external.foo.bar/MyApplication
http://internal.foo.bar/MyApplication
Run Code Online (Sandbox Code Playgroud)

使用带有mod_proxy的apache http服务器将incomming(外部/内部)请求重定向到servlet容器.配置如下所示:

ProxyPass /MyApplication http://localhost:8080/MyApplication retry=1 acquire=3000 timeout=600 Keepalive=On
ProxyPassReverse /MyApplication http://localhost:8080/MyApplication
Run Code Online (Sandbox Code Playgroud)

我现在面临的问题是某些MyApplication响应依赖于原始请求URL.具体:将为WSDL文档提供具有元素的schemaLocation="<RequestUrl>?xsd=MyApplication.xsd"元素.

使用我当前的配置,它总是看起来像

<xs:import namespace="..." schemaLocation="http://localhost:8080/MyApplication?xsd=MyApplication.xsd"/>
Run Code Online (Sandbox Code Playgroud)

但它应该是

External Request: <xs:import namespace="..." schemaLocation="http://external.foo.bar/MyApplication?xsd=MyApplication.xsd"/>
Internal Request: <xs:import namespace="..." schemaLocation="http://internal.foo.bar/MyApplication?xsd=MyApplication.xsd"/>
Run Code Online (Sandbox Code Playgroud)

我想这是一个常见的要求.但由于我不是apache http服务器及其模块配置的专家,如果有人可以提供一些(详细的)帮助,我会很高兴.

提前致谢!

apache mod-rewrite redirect web-applications mod-proxy

22
推荐指数
2
解决办法
4万
查看次数

避免通过JPA向数据库表插入'null'值

我从JPA2开始,到目前为止感觉相当舒适.但是,当使用默认值为NON NULL数据库字段保留具有null属性值的实体时,我遇到了问题.

我希望能够将实体属性保留为null并让数据库插入默认值.

我目前的设置是使用PostgreSQL的openJPA.

我有这个VERSION数据库表(Vorgabewert =默认值):


     Spalte     |             Typ             |         Attribute
----------------+-----------------------------+----------------------------
 status_        | smallint                    | not null Vorgabewert 0
 time_          | timestamp without time zone | not null
 system_time    | timestamp without time zone | not null Vorgabewert now()
 version        | character varying(20)       | not null
 activationtime | timestamp without time zone |
 importtime     | timestamp without time zone |

我有一个实体(Java DTO),它通过xml配置映射数据库字段('status'除外).

我希望我可以插入一个没有system_timeset 的实体,并期望数据库将当前时间填充为默认值.

JPA构造以下SQL-Query:

INSERT INTO public.version (version, activationtime, importtime, system_time, time_) VALUES (?, ?, ?, …
Run Code Online (Sandbox Code Playgroud)

java postgresql orm jpa jpa-2.0

18
推荐指数
3
解决办法
3万
查看次数

再次选择当前选项卡时收到通知

我实现了一个TabActivity实现的OnTabChangeListener.将在选项卡更改(onTabChanged(String tabId))上通知活动.

如果用户再次选择当前选项卡,是否也可以收到通知?

我想使用此事件来执行当前选项卡内容的"刷新",而不是在选项卡或选项菜单中提供刷新按钮.


这就是我最终解决问题的方式 - 解决方案提示是在MisterSquonk的回答中.

(1)定义一个OnTabReselectListener,它必须由表示选项卡内容的活动实现,并且将在重新选择事件上通知.

/**
 * Interface definition for a callback to be invoked when a current selected tab
 * in a TabHost is selected again.
 */
public interface OnTabReselectListener {

    /**
     * Called when a current visible tab is selected again. Will not be invoked
     * on tab changes.
     */
    void onTabReselect();

}
Run Code Online (Sandbox Code Playgroud)

(2)TabActivity的onCreate()中的每个tabWidget子项的setOnTouchListener(来自MisterSquonk的答案)

for (int i = 0; i < tabWidget.getChildCount(); i++) {
    View v = …
Run Code Online (Sandbox Code Playgroud)

android

16
推荐指数
1
解决办法
1万
查看次数

将Jersey/Jackson配置为不使用@XmlElement字段注释进行JSON字段命名

我正在运行Jersey REST服务.代表我的资源的POJO是JAXB(XML)带注释的简单Java类(它们是从模式定义生成的 - 因此它们具有注释).

我希望Jersey/Jackson忽略XML-Annotations.我在我的web.xml中做了这个配置(如这里提到的):

  <init-param>
    <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
    <param-value>true</param-value>
  </init-param>
Run Code Online (Sandbox Code Playgroud)

我现在预计@ZMLElement注释将不再用于JSON字段命名策略.

但是看看这个java字段(成员)

@XmlElement(name = "person", required = true)
protected List<Person> persons;
Run Code Online (Sandbox Code Playgroud)

我仍然得到以下JSON表示:

....,"person":[{"name":"FooBar", ....... (person without the 's')
Run Code Online (Sandbox Code Playgroud)

所有其他字段仍然从@XmlElement注释中获取其JSON名称,而不是从Java字段名称获取.

我想实现杰克逊完整数据绑定(POJO)示例中描述的JSON输出.

它在这样的简单测试中工作正常(使用我的XML注释类):

  ObjectMapper mapper = new ObjectMapper(); 
  mapper.writeValue(System.out, myObject);
Run Code Online (Sandbox Code Playgroud)

但嵌入在泽西岛我没有得到预期的JSON输出.

他们在Jersey中的其他配置选项是否获得"简单"POJO JSON表示(因为这最适合必须反序列化JSON结果的客户端).

谢谢克劳斯

详细解决方案

(1)ContextResolver为Jacksons 实现一个ObjectMapper创建不使用注释的ObjectMapper的Jacksons .

package foo.bar.jackson;

import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.ext.ContextResolver;
import javax.ws.rs.ext.Provider;

import org.codehaus.jackson.map.DeserializationConfig;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.SerializationConfig;

/**
 * Customized {@code ContextResolver} implementation that does not use any …
Run Code Online (Sandbox Code Playgroud)

java json jersey jackson

14
推荐指数
1
解决办法
2万
查看次数

在eclipse"Run As - > Android Application"构建中包含maven依赖项

我使用eclipse,Android开发工具(ADT)插件maven-android-plugin开发了一个Android应用程序.

在命令行(mvn install/ adb install)上构建和安装应用程序可以正常工作 - 尤其是它包含pom.xml中指定的所有依赖项.

通过eclipse将应用程序部署到Android(运行方式 - > Android应用程序)不会自动将依赖项放在apk文件中 - 尽管在pom.xml中指定.我没有在eclipse项目Build Path -> Libraries对话框中明确声明它们.

有没有办法让eclipse包含pom.xml中定义的依赖项,而无需在项目Build-Path - > Libraries中定义它们两次?

编辑:澄清 - eclipse项目包含一个"Maven Dependencies"条目,其中包含pom.xml中指定的所有依赖项.但是在安装过程中,这些依赖项不会通过"运行方式 - > Android应用程序"包含在apk中.要包含它们,必须在"Java Build Path - > Libraries"对话框中额外添加每个.那就是痛苦!

eclipse android maven

13
推荐指数
1
解决办法
9782
查看次数

java.time:CET时区是否考虑夏令时?

我使用java.timeJava 8 的新实现,对UTC到CET时间转换结果的输出感到惊讶.

ZonedDateTime utcTime = ZonedDateTime.of(2014, 7, 1, 8, 0, 0, 0, ZoneId.of("UTC"));
ZonedDateTime cetTime = ZonedDateTime.ofInstant(utcTime.toInstant(), ZoneId.of("CET"));
System.out.println("Summer-UTC-Time: " + utcTime);
System.out.println("Summer-CET-Time: " + cetTime);

System.out.println();

utcTime = ZonedDateTime.of(2014, 1, 1, 8, 0, 0, 0, ZoneId.of("UTC"));
cetTime = ZonedDateTime.ofInstant(utcTime.toInstant(), ZoneId.of("CET"));
System.out.println("Winter-UTC-Time: " + utcTime);
System.out.println("Winter-CET-Time: " + cetTime);
Run Code Online (Sandbox Code Playgroud)

我预计CET时间总是+1时间,但我得到了:

Summer-UTC-Time: 2014-07-01T08:00Z[UTC]
Summer-CET-Time: 2014-07-01T10:00+02:00[CET] -> +2 **Unexpected**

Winter-UTC-Time: 2014-01-01T08:00Z[UTC]
Winter-CET-Time: 2014-01-01T09:00+01:00[CET] -> +1 Expected
Run Code Online (Sandbox Code Playgroud)

显然我必须处理夏令时,这是我在使用CET时没想到的.是java.timeCET真理CEST?如果是的话,如果我需要CET,我应该使用哪个区域?

java java-8 java-time

13
推荐指数
1
解决办法
1万
查看次数

在Java 8中使用函数类型congruent lambda表达式

我挣扎着定义和使用

Stream.collect(Supplier<R> supplier, BiConsumer<R,? super T> accumulator, BiConsumer<R,R> combiner)
Run Code Online (Sandbox Code Playgroud)

Java 8中的方法.

方法签名包括BiConsumer类型参数.BiConsumer FunctionalInterface定义了一个函数方法accept(Object,Object).据我所知,我现在可以使用任何与此功能接口一致的 lambda表达式.

但是Stream.collect JavaDoc中提到的示例是例如

 List<String> asList = stringStream.collect(ArrayList::new, ArrayList::add, ArrayList::addAll);
Run Code Online (Sandbox Code Playgroud)

我不明白为什么ArrayList.add(E e)(单个参数)与BiConsumer.accept(T t,U u)方法(两个参数)一致,并且可以用作collect方法中的累加器函数.

如你所见,我显然缺乏理解并欣赏任何解释.

java lambda java-8

10
推荐指数
2
解决办法
757
查看次数