我有一个实体:
class SomeInfo(
@NotNull @Pattern(regexp = Constraints.EMAIL_REGEX) var value: String) {
var id: Long? = null
}
Run Code Online (Sandbox Code Playgroud)
和控制器方法:
@RequestMapping(value = "/some-info", method = RequestMethod.POST)
public Id create(@Valid @RequestBody SomeInfo someInfo) {
...
}
Run Code Online (Sandbox Code Playgroud)
@Valid 注释不起作用.
看起来Spring需要一个默认的无参数构造函数,而上面的花哨代码变得像这样丑陋(但工作):
class SomeInfo() {
constructor(value: String) {
this.value = value
}
@NotNull @Pattern(regexp = Constraints.EMAIL_REGEX)
lateinit var value: String
var id: Long? = null
}
Run Code Online (Sandbox Code Playgroud)
有什么好的做法让它不那么罗嗦?
谢谢.
乍一看我有一个简单的问题:
entityManager()
.createNativeQuery("select count(*) as total, select sum(field) as total_sum ... blabla")
Run Code Online (Sandbox Code Playgroud)
我想将选择结果写入POJO,如下所示:
public class AggregateStatsDto {
private int total;
private long totalSum;
// getters, setters, cosntructors
}
Run Code Online (Sandbox Code Playgroud)
实现这一目标的最佳方法是什么?
我可以使用JPA 2.1,并试图使用@SqlResultSetMapping在同一起选择@ConstructorResult:
@SqlResultSetMapping(name = "AggregateStatsResult", classes = {
@ConstructorResult(targetClass = AggregateStatsDto.class,
columns = {
@ColumnResult(name = "total"),
@ColumnResult(name = "totalSum")
})
})
public class AggregateStatsDto {
private long total;
private int totalSum;
// getters, setters, cosntructors
}
Run Code Online (Sandbox Code Playgroud)
查询:
AggregateStatsDto result = (AggregateStatsDto) entityManager()
.createNativeQuery("select count(*) as total, …Run Code Online (Sandbox Code Playgroud) 我使用Spring的@Valid注释来验证使用注释注释的bean的字段javax.constraints.
但是当我需要从验证中排除某些字段时我遇到了问题(仅在某些情况下).
我做了调查没有找到任何有用的方法,大多数答案的日期为2010-2011.令人惊讶的是,这种情况非常普遍.
春季4. +的时间有变化吗?或者也许任何人都可以分享个人经验如何打败这个?
谢谢.
我有一个主题的服务:
@Injectable() export class UserService() {
private currentUserSubject = new BehaviorSubject<User>(null);
public currentUser = this.currentUserSubject.asObservable().distinctUntilChanged();
... // emitting new User
}
Run Code Online (Sandbox Code Playgroud)
有一个组件我将此服务注入并订阅更新:
@Component() export class UserComponent {
constructor(private userService: UserService) {
this.userService.currentUser
.subscribe((user) => {
// I want to see not null value here
})
}
}
Run Code Online (Sandbox Code Playgroud)
我想应用一些东西来Observable<User>过滤所有空值,只有在User实际加载时才进入订阅.
我有非常简单的代码:
enum Color { BLUE, RED }
class Brush {
color: Color
constructor(values) {
this.color = values.color
}
}
let JSON_RESPONSE = `{"color": "BLUE"}`
let brush = new Brush(JSON.parse(JSON_RESPONSE))
Run Code Online (Sandbox Code Playgroud)
现在我想检查一下:
console.log(brush.color === Color.BLUE)
Run Code Online (Sandbox Code Playgroud)
它返回false。
我尝试了一些组合,例如
brush.color === Color[Color.BLUE]
Run Code Online (Sandbox Code Playgroud)
但是,当然,遇到了编译器错误。
问题是如何进行相当基本的比较enum === enum?
客户端在服务器上发送(实现无关紧要):
/path/items/ + urlencode(id, SOME_ENCODING)
Run Code Online (Sandbox Code Playgroud)
考虑结果URL将是:
/path/items/my%2Fkey
Run Code Online (Sandbox Code Playgroud)
因此我在服务器上:
@RequestMapping(value = "/path/items/{identifier}", method = RequestMethod.GET)
public Item get(@PathVariable String identifier) {
try {
return DAO.getItemByIdentifier(URLDecoder.decode(identifier, SOME_ENCODING))
}
catch (UnsupportedEncodingException e) {
...
}
Run Code Online (Sandbox Code Playgroud)
在内部有没有办法在Spring中做到这一点?我的意思是标识符已经解码,所以我可以:
@RequestMapping(value = "/path/items/{identifier}", method = RequestMethod.GET)
public Item get(@PathVariable String identifier) {
return DAO.getitemByidentifier(identifier); // already decoded!
}
Run Code Online (Sandbox Code Playgroud) 我创建一个43kk行的表,用值1..200填充它们.因此,通过表格传播的每个数字约为220k.
create table foo (id integer primary key, val bigint);
insert into foo
select i, random() * 200 from generate_series(1, 43000000) as i;
create index val_index on foo(val);
vacuum analyze foo;
explain analyze select id from foo where val = 55;
Run Code Online (Sandbox Code Playgroud)
结果:http: //explain.depesz.com/s/fdsm
我希望总运行时间<1s,是否可能?我有SSD,核心i5(1,8),4GB RAM.9,3 Postgres.
如果我使用Index Only扫描,它的工作速度非常快:
explain analyze select val from foo where val = 55;
Run Code Online (Sandbox Code Playgroud)
http://explain.depesz.com/s/7hm
但我需要选择id而不是val,所以Incex Only扫描不适合我的情况.
提前致谢!
附加信息:
SELECT relname, relpages, reltuples::numeric, pg_size_pretty(pg_table_size(oid))
FROM pg_class WHERE oid='foo'::regclass;
Run Code Online (Sandbox Code Playgroud)
结果:
"foo";236758;43800000;"1850 …Run Code Online (Sandbox Code Playgroud) 我有Spring Boot后端和Angular2前端.我想分别开发它们并部署到Heroku.
它们不应该有任何共同的依赖关系,应该在单独的git-repos中.
据我了解,有两种主要的实现方式:
运行npm build并将dist文件resource夹复制到Spring应用程序的文件夹中,以便最后将其作为静态内容处理
运行服务器专门为Angularapp 服务,它将与Springapp 通信(这里出现CORS问题?)所以总共有两台服务器
我认为第一种方式有点"脏",因为我不认为从一个项目到另一个项目的复制文件夹是好的.
而第二种方式是矫枉过正,因为我有两个服务器(Tomcat和Node.js,例如).Angular如果我可以简单地放入Angular内部,为什么我应该有应用服务器Spring?
有没有更合适的方式来提出上述内容?
谢谢.
考虑我有一个方法在 ActiveRecord 模式样式中做一些事情和日志记录机制:
@Transactional
public void doSmt() {
try {
someOperation(); // can throw exception
Logger.logIntoDb(); // if everything is OK
} catch {Exception e} {
Logger.logIntoDbWithException(e.getMessage()); // log error into db
throw new MyCustomException(e);
}
}
public static void logIntoDbWithException(String message) {
final LogEntry logEntry = new LogEntry();
logEntry.setDate(new Date());
logEntry.setMessage(message);
logEntry.persist();
}
Run Code Online (Sandbox Code Playgroud)
我想在失败的情况下保留一条错误消息,但是如果我在 catch 子句中重新抛出异常,事务将被回滚并且我的 LogEntry 将不会被保留。我看到的唯一方法是flush()在persist().
是否有更干净的解决方案来执行此操作?
谢谢。
更新:
由于我有一个执行持久化的静态方法,因此我需要将以下 hack 应用于已接受的答案:
public static void logIntoDbWithException(String message) {
new Runnable() {
@Transactional(propagation = …Run Code Online (Sandbox Code Playgroud) 我有一些应该从抛出的 InvocationTargetException 中检索的自定义异常,我按以下方式进行:
try {
...
}
catch (IllegalAccessException | InvocationTargetException
| NoSuchMethodException | NoSuchFieldException e) {
if (e.getCause() instanceof CustomException) {
throw (CustomException) e.getCause();
}
throw new IllegalArgumentException();
}
Run Code Online (Sandbox Code Playgroud)
但是 findbugs 向我抱怨:
来自 Throwable 的未经检查/未经确认的演员表
我发现了一个 silimar 问题(如何解决 dodgy:unchecked/unconfirmed cast in sonar?),但它没有帮助。
目前我正在阅读Java 8 Lambdas:实用功能编程(非常有趣且写得很好的书,没有.).
在第6章之后有一个练习:
代码将列表中的每个数字相乘,并将结果乘以5.这样可以顺序执行,但在并行运行时会出错.
public static int multiplyThrough(List<Integer> linkedListOfNumbers) {
return linkedListOfNumbers.parallelStream()
.reduce(5, (acc, x) -> x * acc);
}
Run Code Online (Sandbox Code Playgroud)
和解决方案:
public static int multiplyThrough(List<Integer> numbers) {
return 5 * numbers.parallelStream()
.reduce(1, (acc, x) -> x * acc);
}
Run Code Online (Sandbox Code Playgroud)
我不明白为什么第一种方法在并行模式中是错误的,并且书中没有解释.
请解释.
我想以Java 8风格实现以下代码:
public Path getSomeParent(Path path, int depth) {
Path someParent = path;
for (int i = 0; i < depth; i++) {
someParent = someParent.getParent();
}
return someParent;
}
Run Code Online (Sandbox Code Playgroud)
但是到目前为止没有运气,尽管我认为这个问题非常普遍.
第一个想法是用它做reduce但getParent()不是联想,不是吗?
第二个是使用,IntStream.range(0, depth)但我找不到用它来完成这项任务的方法.它适用于返回的方法void,就是这样.
提前致谢.
考虑我有一个像这样的字符串:
Hi, <@W12313>, <@U333111>!
我需要提取所有与模式匹配的内容,<@([WU].+?)>.+这样我就会得到["W12313", "U333111"]. 我成功匹配了一个,但我不知道如何将其提取到列表中。
我使用Java风格。
java ×8
spring ×4
angular ×3
hibernate ×2
java-8 ×2
java-ee ×2
javascript ×2
jpa ×2
lambda ×2
spring-mvc ×2
sql ×2
typescript ×2
annotations ×1
enums ×1
exception ×1
findbugs ×1
heroku ×1
java-stream ×1
jdbc ×1
json ×1
kotlin ×1
node.js ×1
npm ×1
observable ×1
performance ×1
postgresql ×1
regex ×1
rest ×1
rxjs ×1
spring-boot ×1
url ×1