我有一个 Wrapper 类,它会导致在包装对象上调用 equalsWithoutId 方法,而不是 equals 方法。此处实现:
import org.apache.commons.lang3.Validate;
public class IdAgnosticWrapper {
private final IdAgnostic wrapped;
public IdAgnosticWrapper(IdAgnostic wrapped) {
Validate.notNull(wrapped, "wrapped must not be null");
this.wrapped = wrapped;
}
@Override
public int hashCode() {
return wrapped.hashCodeWithoutId();
}
@Override
public boolean equals(Object obj) {
if (!(obj instanceof IdAgnosticWrapper)) {
return false;
}
return wrapped.equalsWithoutId(((IdAgnosticWrapper) obj).getWrapped());
}
public IdAgnostic getWrapped() {
return wrapped;
}
}
Run Code Online (Sandbox Code Playgroud)
IdAgnostic 是一个简单的接口,可确保存在所需的方法
public interface IdAgnostic {
int hashCodeWithoutId();
boolean equalsWithoutId(Object o);
}
Run Code Online (Sandbox Code Playgroud)
然后我有一些单元测试应该测试 equals() 委托给wrapped#equalsWithoutId() …
在示例应用程序中,我有一个简单的@RestController控制器:
package app.springtest.api.book;
import app.springtest.service.BookService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.validation.Valid;
@RestController
@RequestMapping("api/v1/book")
@Slf4j
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class BookController {
private final BookService bookService;
@PostMapping
public ResponseEntity upsertBook(@Valid @RequestBody BookRequest bookRequest) {
final BookResponse response = bookService.addBook(bookRequest);
return ResponseEntity.ok().body(response);
}
}
Run Code Online (Sandbox Code Playgroud)
它消耗了这个Json对象。
package app.springtest.api.book;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import lombok.experimental.Wither;
import javax.validation.constraints.NotBlank;
@Data
@RequiredArgsConstructor(onConstructor = @__(@JsonCreator))
@Wither
public class BookRequest {
@NotBlank
@JsonProperty(value …
Run Code Online (Sandbox Code Playgroud) 我使用JAXB来取消/编组从服务器获取的XML消息.通常我会在字段中获取XMLGregorianCalendar值,这些值在描述XSD文件中定义为xs:dateTime,因此转换为XMLGregorianCalendar由JAXB自动完成.
来自XSD文件的示例
<xs:attribute name="readouttime" use="required" type="xs:dateTime" />
Run Code Online (Sandbox Code Playgroud)
但是,一个字段被定义为xs:string,如下所示:
<xs:element minOccurs="1" maxOccurs="1" name="Value" type="xs:string" />
Run Code Online (Sandbox Code Playgroud)
但是我收到一个代表dateTime的值:
<Value>2014-08-31T15:00:00Z</Value>
Run Code Online (Sandbox Code Playgroud)
有没有什么好方法,如何将此字符串转换为XMLGregorianCallendar,还是应该使用SimpleDateFormat并手动键入模式?我觉得这可能是一个危险的部分.
我正在通过一个简单的方面记录方法输入和输出参数。
package com.mk.cache;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import java.util.Arrays;
@Aspect
@Component
public class LoggingAspect {
@Around("within(@com.mk.cache.LoggedIO *) && execution(* *(..))")
public Object logAroundPublicMethod(ProceedingJoinPoint joinPoint) throws Throwable {
String wrappedClassName = joinPoint.getSignature().getDeclaringTypeName();
Logger LOGGER = LoggerFactory.getLogger(wrappedClassName);
String methodName = joinPoint.getSignature().getName();
LOGGER.info("LOG by AOP - invoking {}({})", methodName, Arrays.toString(joinPoint.getArgs()));
Object result = joinPoint.proceed();
LOGGER.info("LOG by AOP - result of {}={}", methodName, result);
return result;
}
}
Run Code Online (Sandbox Code Playgroud)
这是由这个注释附加的。
package com.mk.cache;
public @interface LoggedIO {
} …
Run Code Online (Sandbox Code Playgroud) 我无法通过 Keycloak 8.0.2 上的 REST API 删除领域。
我的 Keycloak 在 localhost:38080 上运行。我遵循了此文档https://www.keycloak.org/docs-api/8.0/rest-api/index.html#_realms_admin_resource,请参阅“删除领域”段落。首先,我在http://localhost:38080/auth/realms/master/protocol/openid-connect/token 处获得了管理员用户的令牌
curl --location --request POST 'localhost:38080/auth/realms/master/protocol/openid-connect/token' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--data-urlencode 'username=admin' \
--data-urlencode 'password=admin' \
--data-urlencode 'client_id=vga_tests' \
--data-urlencode 'grant_type=password'
Run Code Online (Sandbox Code Playgroud)
然后我使用令牌并尝试example
通过此调用删除领域
curl --location --request DELETE 'localhost:38080/auth/Example' \
--header 'Authorization: Bearer <TOKEN HERE>' \
--header 'Content-Type: application/json' \
--data-raw ''
Run Code Online (Sandbox Code Playgroud)
(注:curl命令是导出的Postman调用)
我收到了 404 响应
{
"error": "RESTEASY003210: Could not find resource for full path: http://localhost:38080/auth/Example"
}
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
在 Eclipse Luna 中,我打开了两个控制台视图。我将外部构建工具的输出附加到第一个工具,将 Liferay 服务器输出附加到第二个工具。每当我运行外部构建工具时,两个控制台视图都开始显示外部构建工具的输出,并且每次我都必须手动切换第二个控制台视图的内容。是否可以将控制台视图与特定输出永久绑定?我在Eclipse文档中没有找到这个功能。
我在Java中有任何utils方法可以让我用另一个字符串包围一个字符串吗?就像是:
surround("hello","%");
Run Code Online (Sandbox Code Playgroud)
哪会回来 "%hello%"
我只需要一个方法,所以代码会更好,然后添加前缀和后缀.如果没有必要,我也不想拥有自定义的utils类.
我们是用户团队,在 Windows 上工作,我们的 Remote 在 Bitbucket (Linux/UNIX) 中,我们的应用程序正在部署到 Linux 机器上。我们没有注意行尾,直到有一天我们发现,我们笔记本电脑上的 .sh 脚本有 CRLF 行尾。我们决定将 core.autocrlf 设置为 false,因此 Remote 上的行尾与 Windows 笔记本电脑上的行尾没有区别。但是,此选项不会更改我们本地源代码中的 CRLF。
有什么办法,如何告诉 Git 更新所有文件,以便 CRLF 将更改为 LF 就像它在 Remote 上一样?某种重新下载,甚至会下载未更改的文件。
我需要将一个对象列表转换为一个对象属性集的映射,我想为此使用 Java 流。
例如,有一个List<Dog>
,我想将它转换为一个HashMap
,其中关键是狗的年龄,价值是Set
品种。
@Getter //lombok for getters
public class Dog {
private String name;
private int age;
private int breed;
}
Run Code Online (Sandbox Code Playgroud)
我可以Map<Integer, Set<Dog>> dogsByAge
使用此语句创建一个。
Map<Integer, Set<Dog>> dogsByAge = dogs.stream()
.collect(groupingBy(Dog::getAge, toSet()));
Run Code Online (Sandbox Code Playgroud)
不过我需要 Map<Integer, Set<String>> breedsByAge
可以使用流吗?
如何配置 Eclipse Formatter 以添加this.
到每个班级成员使用之前?我在格式化程序配置中找不到这个,谷歌没有给我合理的答案。
我需要这个:
public class Foo {
private int bar;
public void methodUsingBar(){
bar = 1;
}
}
Run Code Online (Sandbox Code Playgroud)
要格式化为:
public class Foo {
private int bar;
public void methodUsingBar(){
this.bar = 1;
}
}
Run Code Online (Sandbox Code Playgroud) java ×7
eclipse ×2
aop ×1
caching ×1
formatting ×1
git ×1
jackson ×1
java-stream ×1
jaxb ×1
keycloak ×1
lombok ×1
mockito ×1
repository ×1
spring ×1
spring-boot ×1
spring-mvc ×1
testng ×1
windows ×1
xml ×1
xsd ×1