我最近开始研究Go并面临下一期.我想实现Comparable接口.我有下一个代码:
type Comparable interface {
compare(Comparable) int
}
type T struct {
value int
}
func (item T) compare(other T) int {
if item.value < other.value {
return -1
} else if item.value == other.value {
return 0
}
return 1
}
func doComparison(c1, c2 Comparable) {
fmt.Println(c1.compare(c2))
}
func main() {
doComparison(T{1}, T{2})
}
Run Code Online (Sandbox Code Playgroud)
所以我收到了错误
cannot use T literal (type T) as type Comparable in argument to doComparison:
T does not implement Comparable (wrong type for compare method)
have …Run Code Online (Sandbox Code Playgroud) I have my entity with few fields and one of them with @Transient annotation.
class Entity {
private String id;
...
@Transient
private String field;
}
Run Code Online (Sandbox Code Playgroud)
And I have next problem with validation. When I try to rejectValue for that field I'm getting next exception: org.springframework.beans.NotReadablePropertyException: Invalid property 'field' of bean class [com.test.Entity]: Bean property 'field' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter?
I've …
我正在使用 Google Cloud Storage 进行文件上传。
我使用了此处提供的简单代码示例https://cloud.google.com/java/getting-started/using-cloud-storage一切都按预期工作。
但是现在我决定使用进度指示器改进我的上传过程?是否可以通过 Java API 获得进度?
我使用 NodeJs 来实现 AWS Lambda 函数,并想阐明测试与 DynamoDB 集成的正确方法是什么:
拉姆达代码:
const AWS = require('aws-sdk');
AWS.config.update({region: region});
const dynamoDb = new AWS.DynamoDB();
exports.handler = async function(event, context) {
...
await dynamoDb.deleteItem(item).promise();
}
Run Code Online (Sandbox Code Playgroud)
我打算mocha, sinon, chai and aws-sdk-mock用于测试:
const expect = require('chai').expect;
const AWS = require('aws-sdk-mock');
const lambda = require('../index.js');
const sinon = require('sinon');
describe('Test', function () {
let deleteItemSpy;
beforeEach(function () {
deleteItemSpy = sinon.spy();
AWS.mock('DynamoDB', 'deleteItem', deleteItemSpy);
}
it('valid call', async function() {
await lambda.handler({"id":1}, null);
expect(deleteItemSpy.calledOnce).to.be.true;
})
}); …Run Code Online (Sandbox Code Playgroud) 我正在尝试执行一个查询,该查询将首先按字段计数聚合,然后通过bin(1h)例如我想得到如下结果:
# Date Field Count
1 2019-01-01T10:00:00.000Z A 123
2 2019-01-01T11:00:00.000Z A 456
3 2019-01-01T10:00:00.000Z B 567
4 2019-01-01T11:00:00.000Z B 789
Run Code Online (Sandbox Code Playgroud)
不确定是否可能,查询应该是这样的:
fields Field
| stats count() by Field by bin(1h)
Run Code Online (Sandbox Code Playgroud)
任何想法如何实现这一目标?
我有两节课。首先是第二类领域。
class A {
@JsonSerializer(using = CustomBSerializer.class)
private B b;
}
class B {
...
}
Run Code Online (Sandbox Code Playgroud)
我有两个自定义序列化器:
class CustomBSerializer extends JsonSerializer<B> {
...
}
class CustomASerializer extends JsonSerializer<A> {
@Override
public void serialize(A a, JsonGenerator jgen, SerializerProvider provider)
throws IOException, JsonProcessingException {
jgen.writeStartObject();
//write here
jgen.writeEndObject();
}
}
Run Code Online (Sandbox Code Playgroud)
我应该添加什么方法而不是注释来写入CustomASerializer字段的序列化值?bCustomBSerializer
我的应用程序中有下一个端点:
@GetMapping(value = "/users")
public Mono<ServerResponse> users() {
Flux<User> flux = Flux.just(new User("id"));
return ServerResponse.ok()
.contentType(APPLICATION_JSON)
.body(flux, User.class)
.onErrorResume(CustomException.class, e -> ServerResponse.notFound().build());
}
Run Code Online (Sandbox Code Playgroud)
目前我可以看到文本"data:"作为一个正文和Content-Type ?text/event-stream邮差.据我所知,Mono<ServerResponse>总是返回数据SSE(Server Sent Event).有可能以某种方式查看Postman客户端的响应吗?
我有一个用 Java 和 Kotlin 语言编写的项目,最近我遇到了下一个问题。
假设我们有MyMonth枚举:
public enum MyMonth {
JAN("January"),
FEB("February");
private final String name;
MyMonth(final String name) {
this.name = name;
}
public String getName() {
return name;
}
}
Run Code Online (Sandbox Code Playgroud)
然后在 Kotlin 中,当我们打印月份名称时:
fun main() {
val month = MyMonth.JAN
println(month.name)
}
Run Code Online (Sandbox Code Playgroud)
我们得到:
JAN
这是文档中描述的内容https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-enum/name.html,但实际上名称应该是January。
有没有办法在 Kotlin 中获取 Java 枚举中指定的名称?
UPD:我的 Kotlin 版本是1.3.30-release-170
UPD2:IDEA 甚至向我显示名称来自getName()Java 中定义的方法:

UPD3:当我们明确使用.getName()它时,它可以工作,但看起来有点奇怪
我正在使用 Spring REST Docs 为我们的 API 生成文档。我已经从这里的教程http://docs.spring.io/spring-restdocs/docs/current/reference/html5/添加了所有内容到 build.gradle
ext {
snippetsDir = file('build/generated-snippets')
}
test {
outputs.dir snippetsDir
}
asciidoctor {
attributes 'snippets': snippetsDir
inputs.dir snippetsDir
outputDir "build/asciidoc"
dependsOn test
sourceDir 'src/main/asciidoc'
}
jar {
dependsOn asciidoctor
from ("${asciidoctor.outputDir}/html5") {
into 'static/docs'
}
}
Run Code Online (Sandbox Code Playgroud)
在我这样做之后,gradle build我可以看到在build/asciidoc目录中生成了文件,也在build/generated-snippets.
但是,当我从 IDEA gradle 任务运行bootRun并尝试访问 localhost:8080/docs/index.html 时,我找不到 404。只是为了测试,我尝试将一些index.html文件放在resources/static目录下,然后执行bootRun,我可以访问localhost:8080/index.html文件在那之后。
如果我打开我的 .jar 文件,我可以看到目录下的静态文件,BOOT-INF/classes/static/docs因此它们被打包到 jar 中。
也许有人有同样的问题?
我有以下流代码:
List<Data> results = items.stream()
.map(item -> requestDataForItem(item))
.filter(data -> data.isValid())
.collect(Collectors.toList());
Data requestDataForItem(Item item) {
// call another service here
}
Run Code Online (Sandbox Code Playgroud)
问题是我只想requestDataForItem在流中的所有元素都有效时调用
。例如,如果第一项无效,我不会调用流中的任何元素。
有.allMatch流API中,但它返回一个boolean。我想要做的一样.allMatch不是
.collect当一切都匹配的结果。另外,我只想处理一次流,有两个循环很容易。
Java Streams API 可以做到这一点吗?
我有我的自定义validator,我想为它添加一些错误消息.所以我有下一个代码:
@Override
public boolean isValid(final String label,
final ConstraintValidatorContext constraintValidatorContext) {
constraintValidatorContext.disableDefaultConstraintViolation();
if(label.length() > MAX_LENGTH) {
constraintValidatorContext
.buildConstraintViolationWithTemplate("{error.maxLength}")
.addConstraintViolation();
return false;
}
...
}
Run Code Online (Sandbox Code Playgroud)
我的消息看起来像error.maxLength=You have exceeded max length of {0},所以它有参数maxLength.
在构建约束违规时可以添加它吗?
java ×6
spring ×3
android ×1
aws-cloudwatch-log-insights ×1
chai ×1
es6-promise ×1
go ×1
go-interface ×1
jackson ×1
java-stream ×1
javax ×1
json ×1
kotlin ×1
node.js ×1
postman ×1
sinon ×1
spring-mvc ×1