我正在使用新的 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)
如何为记录添加默认构造函数或有其他方法可以解决此问题?
我们如何使用带有超出我认为代码点 U+265F 的符文文字的表情符号?
a1 := '\u2665'
a2 := '\u1F3A8'
有没有办法将位置较高的表情符号表示为符文文字?
我在使用SHGetKnownFolderPath()函数时遇到了麻烦.我收到以下错误消息:
Type error in argument 1 to 'SHGetKnownFolderPath'; expected 'const struct _GUID *' but found 'struct _GUID'.
在KnowFolders.h我们有以下相关定义:
#define DEFINE_KNOWN_FOLDER(name,l,w1,w2,b1,b2,b3,b4,b5,b6,b7,b8) \
EXTERN_C const GUID name
...
DEFINE_KNOWN_FOLDER(FOLDERID_ProgramFiles,0x905e63b6,0xc1bf,0x494e,0xb2,0x9c,0x65,0xb7,0x32,0xd3,0xd2,0x1a);
Run Code Online (Sandbox Code Playgroud)
我正在使用Pelles C编译器.
这是我的示例代码:
#include <windows.h>
#include <wchar.h>
#include <KnownFolders.h>
#include <shlobj.h>
int wmain(int argc, wchar_t **argv) {
PWSTR path = NULL;
HRESULT hr = SHGetKnownFolderPath(FOLDERID_ProgramFiles, 0, NULL, &path);
if (SUCCEEDED(hr)){
wprintf(L"%ls", path);
}
CoTaskMemFree(path);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
如何修复此错误消息?
编辑我找到了SHGetKnownFolderPath()的代码示例; 所有这些都在没有指针的情况下执行函数.例如:
hr = SHGetKnownFolderPath(FOLDERID_Public, 0, NULL, &pszPath);
if (SUCCEEDED(hr))
{
wprintf(L"FOLDERID_Public: …Run Code Online (Sandbox Code Playgroud) 将Thymeleaf模板文件放在默认设置下src/main/resources/templates对我来说可以。当我想重命名目录时,请说mytemplates;这是行不通的。
当应用程序启动时,我收到无法找到模板位置:classpath:/ templates /(请添加一些模板或检查您的Thymeleaf配置)警告。
当我指向主页时,出现org.thymeleaf.exceptions.TemplateInputException:解决模板“索引”时出错,模板可能不存在,或者任何配置的模板解析器错误都无法访问该模板。
我使用以下Java配置:
package com.zetcode.conf;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Description;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.thymeleaf.spring4.SpringTemplateEngine;
import org.thymeleaf.spring4.view.ThymeleafViewResolver;
import org.thymeleaf.templateresolver.ClassLoaderTemplateResolver;
@Configuration
public class MvcConfig extends WebMvcConfigurerAdapter {
@Bean
@Description("Thymeleaf template resolver serving HTML 5")
public ClassLoaderTemplateResolver templateResolver() {
ClassLoaderTemplateResolver tres = new ClassLoaderTemplateResolver();
tres.setPrefix("classpath:/mytemplates/");
tres.setSuffix(".html");
tres.setCacheable(false);
tres.setTemplateMode("HTML5");
tres.setCharacterEncoding("UTF-8");
return tres;
}
@Bean
@Description("Thymeleaf template engine with Spring integration")
public SpringTemplateEngine templateEngine() {
SpringTemplateEngine templateEngine …Run Code Online (Sandbox Code Playgroud) 我在Jackson生成的XML输出中得到了两个包装器元素。我只想要一个。
我有一个Java bean
@Entity
@Table(name = "CITIES")
@JacksonXmlRootElement(localName = "City")
public class City implements Serializable {
private static final long serialVersionUID = 21L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@JacksonXmlProperty(isAttribute = true)
private Long id;
@JacksonXmlProperty
private String name;
@JacksonXmlProperty
private int population;
// getters, setters etc
}
Run Code Online (Sandbox Code Playgroud)
和一个列表包装器类。
@JacksonXmlRootElement
public class Cities implements Serializable {
private static final long serialVersionUID = 22L;
@JacksonXmlProperty(localName = "City")
@JacksonXmlElementWrapper(localName = "MyCities")
private List<City> cities = new ArrayList<>();
public List<City> getCities() {
return cities;
} …Run Code Online (Sandbox Code Playgroud) 在 C# LINQ 中,我们可以使用 withlet子句临时变量:
var users2 = from user in users
let total = users.Sum(u => u.Salary)
let avg = total / n
where user.Salary > avg
select user;
Run Code Online (Sandbox Code Playgroud)
是否可以一次性在 F# 中执行相同的操作?
let avg = users |> List.averageBy (fun user -> float user.Salary)
let users2 = users |> List.filter (fun user -> user.Salary > int avg)
Run Code Online (Sandbox Code Playgroud)
我只能通过两次不同的手术来做到这一点。
我有一个数字列表.我想要进行以下操作:1*2 + 3*4.但是,reduce操作一次只能使用一个元素.在我的情况下,我需要一次使用两个元素.是否可以使用reduce或使用任何其他方法?
package com.zetcode
fun main(args: Array<String>) {
val nums = listOf(1, 2, 3, 4)
val res = nums.reduce { total, next -> total * next }
println(res)
}
Run Code Online (Sandbox Code Playgroud)