我正在使用Spring Boot(最新版本,1.3.6),我想创建一个REST端点,它接受一堆参数和一个JSON对象.就像是:
curl -X POST http://localhost:8080/endpoint \
-d arg1=hello \
-d arg2=world \
-d json='{"name":"john", "lastNane":"doe"}'
Run Code Online (Sandbox Code Playgroud)
在我正在做的Spring控制器中:
public SomeResponseObject endpoint(
@RequestParam(value="arg1", required=true) String arg1,
@RequestParam(value="arg2", required=true) String arg2,
@RequestParam(value="json", required=true) Person person) {
...
}
Run Code Online (Sandbox Code Playgroud)
该json参数不会被序列化为Person对象.我得到了
400 error: the parameter json is not present.
Run Code Online (Sandbox Code Playgroud)
显然,我可以将json参数作为String并解析控制器方法中的有效负载,但这种方式无视使用Spring MVC.
如果我使用它,它都可以工作@RequestBody,但是我放弃了在JSON主体之外POST单独的参数的可能性.
Spring MVC中有没有办法"混合"普通的POST参数和JSON对象?
我有一个简单的POJO:
public class ADate {
private Integer day;
private Integer month;
private Integer year;
... // getters/setters/constructor
}
Run Code Online (Sandbox Code Playgroud)
以下JSON文档被正确反序列化为ADate:
{
"day":"10",
"month":"2",
"year":"1972"
}
Run Code Online (Sandbox Code Playgroud)
Jackson自动将String转换为Integer.
有没有办法避免这种自动转换,如果将Integer值定义为String,则让Jackson失败.
我有一个Spring Boot 1.4应用程序,它有一个基于YAML的配置文件.配置文件指定默认配置文件:prod
spring:
profiles.active: prod
Run Code Online (Sandbox Code Playgroud)
当我使用Maven生成战争并部署到Tomcat 7时,未设置默认配置文件.Tomcat日志:
No active profile set, falling back to default profiles: default
Run Code Online (Sandbox Code Playgroud)
为了在Tomcat中部署的应用程序中设置默认配置文件,我可以将System属性传递给Tomcat,如下所示:
-Dspring.profiles.active=prod
Run Code Online (Sandbox Code Playgroud)
这有效,但我想使用Spring Boot属性,而不是系统属性来设置默认配置文件.在Tomcat中部署应用程序时,是否有理由忽略默认的yaml属性文件?
这个问题可能是这个较旧问题的重复。
我正在使用Spring Boot 1.4应用程序,并且有一种用注释的bean方法@Scheduled。我需要将cron值传递给注释,并且由于我使用的是基于YAML的配置,因此cron值存储在YAML文件(application.yaml)中。
我找不到将属性值传递app.cron给注释的方法。
例如,这不起作用
@Scheduled(cron = ${app.cron})
Run Code Online (Sandbox Code Playgroud)
我也尝试过使用EL表达式,但是没有运气。
将基于YAML的属性值传递给Spring注释的正确方法是什么?
我创建了一个简单的 Kafka 消费者,它返回一个Flux对象(收到的消息),我正在尝试使用StepVerifier.
在我的测试中,我做这样的事情:
Flux<Pojo> flux = consumer.start();
StepVerifier.create(flux)
.expectNextMatches(p -> p.getList().size() == 3)
.verifyComplete();
Run Code Online (Sandbox Code Playgroud)
断言工作正常(如果我将值更改为其他值3,则测试失败)。但是,如果断言通过,则测试永远不会退出。
我也尝试过使用这样的verify方法:
StepVerifier.create(flux)
.expectNextMatches(f -> f.getEntitlements().size() == 3)
.expectComplete()
.verify(Duration.ofSeconds(3));
Run Code Online (Sandbox Code Playgroud)
在这种情况下,我收到此错误:
java.lang.AssertionError: VerifySubscriber timed out on false
Run Code Online (Sandbox Code Playgroud)
知道我做错了什么吗?
我正在实施策略模式:
public interface Stuff<T> {
T getStuff();
}
public class IntegerStuff implements Stuff<Integer> {
public Integer getStuff() { .. }
}
public class StringStuff implements Stuff<String> {
public String getStuff() { .. }
}
Run Code Online (Sandbox Code Playgroud)
现在,我想使用“上下文”来设置策略并执行策略方法:
public class Context() {
private Stuff stuff;
public setStrategy(Stuff stuff) { this.stuff = stuff; }
public Object doStuff() { // ARGH!
return stuff.getStuff()
}
}
Run Code Online (Sandbox Code Playgroud)
如何使用泛型,以便doStuff()Context 类上的方法类型可以与使用的策略类型相同?
我有一个 Spring Batch 应用程序 (3.0.7),它通过 Spring Boot 启动,它并行读取多个 XML 文件,处理它们,并针对 Oracle DB“吐出”INSERT 或 UPDATE 语句。
为了并行处理文件,我使用了Partitioner. 这项工作工作正常,除了JdbcWriter似乎只绑定到一个线程的 。由于我使用的是ThreadPoolTaskExecutor,我希望 Step 可以为读取器、处理器和写入器并行运行。但是,似乎 JdbcWriter 总是绑定到Thread-1(我可以在日志中看到,而且还分析了数据库连接,只有一个连接处于活动状态 - 请注意,我的数据源配置为使用具有 20 个连接的池)。
我已将读取器、处理器和写入器注释为 @StepScope。如何有效地使用所有配置的线程来并行taskExecutor读取和写入?
这是我的配置的摘录:
@Bean
public Job parallelJob() throws Exception {
return jobBuilderFactory.get("parallelJob")
.start(splitFileStep())
.next(recordPartitionStep())
.build();
}
@Bean
public Step recordPartitionStep() {
return stepBuilderFactory.get("factiva-recordPartitionStep")
.partitioner(recordStep())
.partitioner("recordStep", recordPartitioner(null)) <!-- this is used to inject some data from the job context
.taskExecutor(taskExecutor())
.build();
}
@Bean …Run Code Online (Sandbox Code Playgroud) java ×3
spring ×3
spring-boot ×3
generics ×1
jackson ×1
json ×1
rest ×1
spring-batch ×1
spring-mvc ×1
tomcat7 ×1