我有一个继承自其他的实体.另一方面,我使用lombok项目来减少样板代码,所以我把@Data
注释.@Data
带继承的注释会产生下一个警告:
生成equals/hashCode实现但不调用超类,即使此类不扩展java.lang.Object.如果这是故意的,请添加
@EqualsAndHashCode(callSuper=false)
到您的类型.
是否可以添加注释@EqualsAndHashCode (callSuper = true)
或@EqualsAndHashCode (callSuper = false)
?如果没有添加,是哪一个callSuper=false
或callSuper=true
?
当spring boot启动时,抛出method names must be tokens
异常
2016-08-11 16:53:54.499 INFO 14212 --- [0.1-8888-exec-1] o.apache.coyote.http11.Http11Processor : Error parsing HTTP request header
Note: further occurrences of HTTP header parsing errors will be logged at DEBUG level.
java.lang.IllegalArgumentException: Invalid character found in method name. HTTP method names must be tokens
at org.apache.coyote.http11.Http11InputBuffer.parseRequestLine(Http11InputBuffer.java:462) ~[tomcat-embed-core-8.5.4.jar!/:8.5.4]
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:994) ~[tomcat-embed-core-8.5.4.jar!/:8.5.4]
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66) [tomcat-embed-core-8.5.4.jar!/:8.5.4]
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:785) [tomcat-embed-core-8.5.4.jar!/:8.5.4]
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1425) [tomcat-embed-core-8.5.4.jar!/:8.5.4]
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) [tomcat-embed-core-8.5.4.jar!/:8.5.4]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [na:1.8.0_72]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [na:1.8.0_72]
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-embed-core-8.5.4.jar!/:8.5.4]
at java.lang.Thread.run(Thread.java:745) [na:1.8.0_72]
2016-08-11 16:53:58.885 …
Run Code Online (Sandbox Code Playgroud) 如果我们使用Java 8 Stream就像list.stream().filter(....).collect(..).....
它什么时候关闭这个流?
作为下一个例子,我们关闭流我们是好的做法吗?
Stream<String> stream = list.stream();
String result = stream.limit(10).collect(Collectors.joining(""));
stream.close();
Run Code Online (Sandbox Code Playgroud) 如果我想@Qualifier
在构造函数依赖注入上使用注释,我会有以下内容:
public class Example {
private final ComponentExample component;
@Autowired
public Example(@Qualifier("someComponent") ComponentExample component) {
this.component = component;
}
}
Run Code Online (Sandbox Code Playgroud)
我知道lombok注释减少样板代码而不必包含构造函数如下:@RequiredArgsConstructors(onConstructor=@__(@Inject))
但这仅适用于没有限定符的属性.
有人知道是否可以添加限定符@RequiredArgsConstructor(onConstructor = @__(@Autowired))
?
我正在使用Netflix Feign将微服务A的一个操作调用到微服务B的其他操作,该操作使用Spring Boot验证代码.
如果验证不好,微服务B的操作会抛出异常.然后我在微服务中处理并返回下一个HttpStatus.UNPROCESSABLE_ENTITY
(422):
@ExceptionHandler({
ValidateException.class
})
@ResponseStatus(HttpStatus.UNPROCESSABLE_ENTITY)
@ResponseBody
public Object validationException(final HttpServletRequest request, final validateException exception) {
log.error(exception.getMessage(), exception);
error.setErrorMessage(exception.getMessage());
error.setErrorCode(exception.getCode().toString());
return error;
}
Run Code Online (Sandbox Code Playgroud)
因此,当微服务A在接口中调用B作为下一个:
@Headers("Content-Type: " + MediaType.APPLICATION_JSON_UTF8_VALUE)
@RequestLine("GET /other")
void otherOperation(@Param("other") String other );
@Headers("Content-Type: " + MediaType.APPLICATION_JSON_UTF8_VALUE)
@RequestLine("GET /code/validate")
Boolean validate(@Param("prefix") String prefix);
static PromotionClient connect() {
return Feign.builder()
.encoder(new GsonEncoder())
.decoder(new GsonDecoder())
.target(PromotionClient.class, Urls.SERVICE_URL.toString());
}
Run Code Online (Sandbox Code Playgroud)
并且验证失败它返回内部错误500并显示下一条消息:
{
"timestamp": "2016-08-05T09:17:49.939+0000",
"status": 500,
"error": "Internal Server Error",
"exception": "feign.FeignException",
"message": "status 422 reading …
Run Code Online (Sandbox Code Playgroud) 我怎样才能转换java.lang.reflect.Type
成Class<T> clazz
?
如果我有一个方法接下来有一个参数Class<T>
:
public void oneMethod(Class<T> clazz) {
//Impl
}
Run Code Online (Sandbox Code Playgroud)
然后另一个方法有一个参数java.lang.reflect.Type
和它调用oneMethod(Class<T> clazz)
,我需要转换java.lang.reflect.Type type
为Class<T>
:
public void someMehtod(java.lang.reflect.Type type) {
// I want to pass type arg to other method converted in Class<T>
otherMethod(¿How to convert java.lang.reflect.Type to Class<T>?);
}
Run Code Online (Sandbox Code Playgroud)
可能吗?
我是新手使用hamcrest.当我发现如何使用它时,我一直怀疑何时使用is
或equalTo
.
有什么区别is
和equalTo
,虽然它在概念上还是ocasionally?它似乎行为相同.
Assert.assertThat(actual, equalTo("blue"));
Assert.assertThat(actual, is("red"));
Run Code Online (Sandbox Code Playgroud)
为什么你会使用一个而不是另一个?
我正在验证我的数据访问对象类的字段。在一次尝试中,我开始向属性(@NotNull、@NotBlank、@Min、@Max 等)添加 Bean 验证注释。我还有更多注释 Jackson (JsonProperty (..)) 用于 swagger 库和文档(@Api (...))。在我看来,这个类非常“脏”,有很多注释(每个属性至少有三个注释)。一个字段的示例:
@JsonProperty("ownName")
@Api(description="it is my own name" required=true)
@Valid
@NotNull
private SomeObject object;
Run Code Online (Sandbox Code Playgroud)
在另一次尝试中,我使用 SpringValidator
接口执行了我自己的验证。如果使用自定义验证器,例如 Spring 接口,它似乎更清晰,并且还允许您自由地为不同情况生成多个验证器。此外,该类似乎没有过多的注释和验证独立于该类。示例Validator
:
public class UserValidator implements Validator {
@Override
public boolean supports(Class<?> arg0) {
return User.class.isAssignableFrom(arg0);
}
@Override
public void validate(Object obj, Errors error) {
User user = (User) obj;
if(user.getPassword().length() < 10)
{
error.reject("Password must be lesser than 10");
}
//more validations....
}
}
Run Code Online (Sandbox Code Playgroud)
我尝试docker for Windows 10
使用next命令创建一个docker-machine :
docker-machine create --driver hyperv default
Run Code Online (Sandbox Code Playgroud)
但我得到了下一个错误
Error with pre-create check: "Hyper-v commands have to be run as an Administrator"
Run Code Online (Sandbox Code Playgroud)
任何人都知道如何解决这个问题?谢谢
我有一个像下一个类的对象列表:
class A {
private String property1;
private String property2;
//Setters && Getters
}
Run Code Online (Sandbox Code Playgroud)
因此,在一些操作之后,我需要使用默认值更新列表,其中包含一些逻辑,如下所示:
listOfA.forEach(item -> {
item.setProperty1(findSomething());
}
Run Code Online (Sandbox Code Playgroud)
这个逻辑重复了几次,所以我想把它导出到一个方法中。所以,我的问题与此方法有关:我应该使用 void 方法更新列表的副本引用,返回它还是创建新列表并更新它?:
选项 1:更新列表的副本引用
private void updateList(List<A> listOfA) {
listOfA.forEach(item -> {
item.setProperty1(findSomething());
}
}
Run Code Online (Sandbox Code Playgroud)
选项2:退货
private List<A> updateList(List<A> listOfA) {
listOfA.forEach(item -> {
item.setProperty1(findSomething());
}
return listOfA;
}
Run Code Online (Sandbox Code Playgroud)
选项 3:从另一个创建一个新列表,更新它并返回它
private List<A> updateList(List<A> listOfA) {
List<A> newList = new ArrayList<>();
//Logic to copy listOfA in newList....
newList.forEach(item -> {
item.setProperty1(findSomething());
}
return newList ;
}
Run Code Online (Sandbox Code Playgroud) java ×9
spring ×4
lombok ×2
coding-style ×1
docker ×1
generics ×1
hamcrest ×1
java-8 ×1
java-stream ×1
spring-boot ×1
validation ×1