我正在使用新的 Java 14 和 Spring Boot。对于数据持有者,我使用了新的很酷的记录,而不是常规的 Java 类。
public record City(Long id, String name, Integer population) {}
Run Code Online (Sandbox Code Playgroud)
稍后在我的服务类中,我使用 SpringBeanPropertyRowMapper来获取数据。
@Override
public City findById(Long id) {
String sql = "SELECT * FROM cities WHERE id = ?";
return jtm.queryForObject(sql, new Object[]{id},
new BeanPropertyRowMapper<>(City.class));
}
Run Code Online (Sandbox Code Playgroud)
我最终出现以下错误:
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.zetcode.model.City]: No default constructor found; nested exception is java.lang.NoSuchMethodException: com.zetcode.model.City.<init>()
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:145) ~[spring-beans-5.2.3.RELEASE.jar:5.2.3.RELEASE]
Run Code Online (Sandbox Code Playgroud)
如何为记录添加默认构造函数或有其他方法可以解决此问题?
I have Java 19, and I am attempting to do some simple pattern-matching on a record that I created. However, Java is giving me a very confusing compilation error. Here is the simplest example I could make that causes the error.
public class ExpressionTypeIsASubsetOfPatternType
{
public record Triple(int a, int b, int c) {}
public static void main(String[] args)
{
System.out.println("Java Version = " + System.getProperty("java.version"));
final Triple input = new Triple(1, 2, 3);
if (input instanceof Triple t)
{ …Run Code Online (Sandbox Code Playgroud) Java记录中定义的属性的最大数量(技术上是 \xe2\x80\x9crecord_components\xe2\x80\x9d)是多少?
\n我阅读了规范JEP 395:记录。没有提到限制。
\n我正在重构我的代码。我想在我的 DTO 中使用 java 记录而不是 java 类。要将 DTO 转换为实体,我使用的是 ModelMapper(2.3.5 版)。当我尝试获取有关用户的信息(调用方法将实体转换为 DTO)时,出现此错误。
Failed to instantiate instance of destination xxx.UserDto. Ensure that xxx.UserDto has a non-private no-argument constructor.
这是我的代码。
public record UserDto(String firstName,
String lastName,
String email,
String imageUrl) {}
Run Code Online (Sandbox Code Playgroud)
@RestController
public class UserController {
@Autowired
private UserRepository userRepository;
@Autowired
private ModelMapper modelMapper;
@GetMapping("/user/me")
@PreAuthorize("hasRole('USER')")
public UserDto getCurrentUser(@CurrentUser UserPrincipal userPrincipal) {
return convertToDto(userRepository.findById(userPrincipal.getId())
.orElseThrow(() -> new ResourceNotFoundException("User", "id", userPrincipal.getId())));
}
private UserDto convertToDto(User user) {
UserDto userDto = modelMapper.map(user, UserDto.class);
return …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 java 14 的新记录功能,目前处于预览阶段。
我知道在 Java 中,每个文件必须有一个公共类,但是新的记录语法非常好且简短,让一堆文件中的每个文件都包含一行代码似乎很浪费。
特别是我想尝试为这样的简单 AST 建模,我认为将所有内容放在一个文件中确实提高了可读性和理解性。
package com.company;
public interface Expression {
}
public record IntExp(int value) implements Expression {
}
public record AddExp(Expression left, Expression right) implements Expression {
}
public record SubtractExp(Expression left, Expression right) implements Expression {
}
// Etc..
Run Code Online (Sandbox Code Playgroud)
但不幸的是,这不会编译。
所以我的问题是:
有没有办法绕过这个限制,或者有什么方法可以将这样的代码全部保存在一个地方?
我无法在 IntelliJ 中创建 Java 记录类,并且找不到启用它的选项。
有办法启用它吗?
我正在使用 Java 15 和 JSF 2.3 以及 PrimeFaces 8 开发一个简单的 JSF Web 应用程序(主要用于学习),并且我正在使用一个简单的 Java 记录来对实体进行建模。该应用程序不使用任何数据库。
我的问题是是否可以将 java 记录用作 xhtml 页面中的值,如下所示
<p:column headerText="Id">
<h:outputText value="#{car.randomId}" />
</p:column>
Run Code Online (Sandbox Code Playgroud)
因为我收到以下错误
javax.el.PropertyNotFoundException: The class 'com.company.Car' does not have the property 'id'.。我试过放置car.year(),但没有用。记录的定义是这个
public record Car (String randomId, String randomBrand, int randomYear, String randomColor, int randomPrice, boolean randomSoldState) {
}
Run Code Online (Sandbox Code Playgroud)
在 pom.xml 中,我使用以下 api
<dependency>
<groupId>jakarta.platform</groupId>
<artifactId>jakarta.jakartaee-api</artifactId>
<version>8.0.0</version>
<scope>provided</scope>
</dependency>
Run Code Online (Sandbox Code Playgroud)
感谢您的帮助!
使用 JDK 16,我声明了两个注释:
@Target({ ElementType.RECORD_COMPONENT})
@Retention(RetentionPolicy.RUNTIME)
public @interface A {}
@Target({ ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface B {}
Run Code Online (Sandbox Code Playgroud)
我声明了一个像这样的记录类?
public record User(@A @B long id, String name, int age) {}
Run Code Online (Sandbox Code Playgroud)
然后我使用反射来获取id的注解,即:
Annotation[] annotations = fields[0].getAnnotations();
Run Code Online (Sandbox Code Playgroud)
但是 的大小annotations是 1 而我只有@B,这是为什么呢?谢谢
我在想这样的事情:
record Foo() implements Cloneable {
public Foo clone() {...}
}
Run Code Online (Sandbox Code Playgroud)
这是一件好事吗?我们应该避免它以支持未来的枯萎吗?
我有一个专门用于在运行时设置字段的测试框架。目的是设置测试用例。期待升级到 Java 14+ 记录,我注意到现有实用程序(例如ReflectionTestUtils.setField和PropertyAccessorFactory.forDirectFieldAccess)适用于普通私有最终字段,但不适用于记录字段。
这是 JVM 的限制,还是这些实用程序的限制?
java-record ×10
java ×9
java-14 ×4
java-16 ×2
reflection ×2
cloneable ×1
el ×1
instanceof ×1
jsf ×1
limit ×1
max ×1
modelmapper ×1
record ×1
spring-boot ×1
testing ×1