我有一个列表myListToParse,我想过滤元素并在每个元素上应用一个方法,并将结果添加到另一个列表中myFinalList.
使用Java 8,我注意到我可以通过两种不同的方式完成它.我想知道他们之间更有效的方式,并理解为什么一种方式比另一种更好.
我对任何有关第三种方式的建议持开放态度.
方法1:
myFinalList = new ArrayList<>();
myListToParse.stream()
.filter(elt -> elt != null)
.forEach(elt -> myFinalList.add(doSomething(elt)));
Run Code Online (Sandbox Code Playgroud)
方法2:
myFinalList = myListToParse.stream()
.filter(elt -> elt != null)
.map(elt -> doSomething(elt))
.collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud) 根据文档,可以为标志定义多个args --build-arg,但我不知道如何.我尝试了以下方法:
docker build -t essearch/ess-elasticsearch:1.7.6 --build-arg number_of_shards=5 number_of_replicas=2 --no-cache .
Run Code Online (Sandbox Code Playgroud)
=>这会返回错误.
我也尝试过:
docker build -t essearch/ess-elasticsearch:1.7.6 --build-arg number_of_shards=5,number_of_replicas=2 --no-cache .
Run Code Online (Sandbox Code Playgroud)
=>这将一个变量设置number_of_shards为值"5,number_of_replicas = 2"
知道如何定义多个参数吗?
我知道该Accept参数定义了从服务器发送的客户端响应中所期望的数据类型,因此它被用作响应头.
我的问题是有关Content-type,它的使用由客户来定义发送的请求的主体格式,我一直把它作为一个客户端请求的一部分,所以我必须在我与设置的标头的客户端请求Accept和Content-type.最近,我遇到了一个项目,其中Content-type在响应标头中定义(因此由服务器发送).所以我的问题是:Content-type需要设置为客户端请求标头的一部分或作为服务器响应标头的一部分,还是可以设置为两者?
让父pom将每个微服务声明为模块是一个好主意吗?所以有助于管理公共依赖项(比如在每个项目中使用lib servlet-api,删除所有项目并仅在父pom中声明它)
我们正在尝试使用Spring-Data-JPA和Spring-Data-Rest的POC.输出看起来是例外,我们有实体字段的值,除了id,id字段在Abstract类中设置.
我们正在使用一个简单的实体,它扩展了AbstractPersistable(它是一个spring-data-jpa类http://docs.spring.io/spring-data/jpa/docs/current/api/org/springframework/data/jpa/domain /AbstractPersistable.html).
这里的源代码如下:
@MappedSuperclass
public abstract class AbstractPersistable<PK extends Serializable> implements Persistable<PK> {
private static final long serialVersionUID = -5554308939380869754L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "ID")
private PK id;
/*
* (non-Javadoc)
*
* @see org.springframework.data.domain.Persistable#getId()
*/
public PK getId() {
return id;
}
/**
* Sets the id of the entity.
*
* @param id the id to set
*/
protected void setId(final PK id) {
this.id = id;
}
/*
* (non-Javadoc)
*
* …Run Code Online (Sandbox Code Playgroud) 任何人都可以解释为什么@Bean静态方法返回2个不同的实例?
我可以理解,@Bean在类非A返回相同实例的非静态方法上,因为默认范围是单例.
如果我尝试注入类B用@Autowire的服务将无法正常工作,所以它看起来像它不是由Spring应用程序上下文加载.所以使用D类似的类会是类似的!我想不是因为@PropertySource我们需要另外使用(用于占位符):
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}
Run Code Online (Sandbox Code Playgroud)
如果我们从中删除@Bean,它将无法工作.
还有其他用例,在静态方法上使用@Bean会有用吗?
例:
当我跑:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {Conf.class})
public class Test {
@org.junit.Test
public void test(){
}
}
Run Code Online (Sandbox Code Playgroud)
对于
@Configuration
@ComponentScan
public class Conf {
@Bean
public A aaa(){
return new A();
}
@Bean
public static B bbb(){
return new B();
}
@Bean
@Scope("prototype")
public C ccc(){
return new C();
}
public static …Run Code Online (Sandbox Code Playgroud) I am currently using a piece of code to set parameters and I do a REST call to a URL using restTemplate, it works fine:
MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>();
map.add("grant_type", grantType);
map.add("client_id", clientId);
map.add("client_secret", clientSecret);
HttpEntity<?> entity = new HttpEntity<Object>(map);
restTemplate.exchange("myurl", HttpMethod.POST, entity, Void.class);
Run Code Online (Sandbox Code Playgroud)
But if I am using a LinkedMultiValueMap it's because I looked on the web ;)
And if I replace it by a HashMap, it works as well, so can anyone tell me …
我正在尝试为控制器中定义的方法编写单元测试代码。方法是这样的:
@RestController
@RequestMapping("/products")
public class RestProductController {
@RequestMapping(value="/{product}/skus", produces = MediaType.APPLICATION_JSON_VALUE, method = RequestMethod.GET)
public List<SkuSummaryVO> getSkuByProduct(@Valid @PathVariable Product product){
List<SkuSummaryVO> skusByProductVOs = skuService.getSkusByProduct(product);
return skusByProductVOs;
}
}
Run Code Online (Sandbox Code Playgroud)
我们在Configuration类中使用注释@EnableSpringDataWebSupport来启用DomainClassConverter功能。因此,我们可以将JPA实体用作@PathVariable。因此,当在URL中设置产品ID时,我们将获得产品(在后台提出请求)。
我们正在开发单元测试,而没有启用Spring App Context并使用Mockito。所以我们像这样初始化了mockMvcBuilders:
public class RestProductControllerTest {
...
@Before
public void setUp() {
RestProductController restProductController = new RestProductController();
...
mockMvc = MockMvcBuilders.standaloneSetup(restProductController).build();
}
}
Run Code Online (Sandbox Code Playgroud)
测试方法是这样的:
@Test
public void testGetProductById() throws Exception {
...
String jsonResult = ...;
mockMvc.perform(get("/products/123/skus").contentType(MediaType.APPLICATION_JSON)).andExpect(status().isOk()).andExpect(content().string(jsonResult));
}
Run Code Online (Sandbox Code Playgroud)
我得到了500的HttpCode(状态)
而且单元测试对于不使用DomainClassConverter功能的控制器方法也可以正常工作(例如,如果我使用a Long productId而不是a Product product作为参数 getSkuByProduct,它将正常工作)
在我们的项目架构中,我们使用经典的 MVC 模式,包括经典的服务层(打开事务并调用 DAO 层)。
对于每个服务,我们都有一个实现和他的接口。但说实话,我很确定对于一个服务和他的接口,我们永远不会有多个实现。好吧,也许在接口中声明公共方法有助于了解服务的作用更清楚,但是一个接口用于有多个实现,如果我们知道我们不会有多个实现,我们应该保持他们?
我有一个我需要手动设置 ID (PK) 的实体。我有一些抽象的 @MappedSuperclass 用于审计和 PK,我仍然想使用它。所以我的想法是覆盖 id 列以摆脱 @GeneratedValue(strategy = GenerationType.AUTO.
所以我有这样的事情:
@Entity
@Table(name = Constants.MERCHANT_PREFIX + "MERCHANT")
@Cacheable(false)
public class Merchant extends AbstractAuditable<String, Long> {
@AttributeOverride(name = "id", column = @Column(name="ID"))
private Long id;
@Override
public Long getId() {
return id;
}
@Override
public void setId(Long id) {
this.id = id;
}
}
@MappedSuperclass
@EntityListeners(value = { AuditingEntityListener.class })
public abstract class AbstractAuditable<U, PK extends Serializable> extends AbstractPersistable<PK> {
...
}
@MappedSuperclass
public abstract class AbstractPersistable<PK extends Serializable> …Run Code Online (Sandbox Code Playgroud) 在 Java 8 中,我找到了一种从值中获取枚举的常用方法,它对Arrays.stream所有枚举值使用带过滤器的过滤器,但最近,我遇到了另一种方法,用Stream.of,彼此之间的更好方法是什么为什么?还有其他更好的方法吗?
例子:
public enum Foo {
BAR_1("Bar 1"),
BAR_2("Bar 2");
private String friendlyValue;
Foo(String friendlyValue){
this.friendlyValue = friendlyValue;
}
public String getFriendlyValue() {
return friendlyValue;
}
public static Foo fromFriendlyValue1(String friendlyValue){
return Stream.of(Foo.values()).filter(r -> r.getFriendlyValue().equals(friendlyValue)).findFirst().get();
}
public static Foo fromFriendlyValue2(String friendlyValue) {
return Arrays.stream(Foo.values()).filter(r -> r.getFriendlyValue().equals(friendlyValue)).findFirst().get();
}
}
Run Code Online (Sandbox Code Playgroud) java ×7
spring ×3
java-8 ×2
jpa ×2
spring-mvc ×2
arguments ×1
build ×1
docker ×1
enums ×1
http-headers ×1
java-stream ×1
junit ×1
maven ×1
rest ×1
spring-test ×1