我正在尝试将字符串的格式解析"yyyy-MM-dd'T'HH:mm:ss'Z'"为LocalDateTime如果日期是sunday或者saturday我想将日期更改为monday相同格式并返回,我知道我可以通过使用添加天数plusDays
String str = "2018-09-22T12:30:10Z";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss'Z'");
LocalDateTime dateTime = LocalDateTime.parse(str, formatter);
System.out.println(dateTime.plusDays(2)); //2018-09-24T12:30:10
Run Code Online (Sandbox Code Playgroud)
但我想以格式返回 2018-09-24T12:30:10Z
我使用的是 Spring Boot 2.0.5 版。我有一种@Bean方法来创建请求范围 bean,但要创建实例,我需要访问我为其创建实例的请求的 http 标头字段。在@RequestHeader注释工作正常,在我@RestController,但不是我的@Bean方法。有谁知道如何在这种情况下访问该信息?
下面是我想做的一个例子,但它不起作用,因为@RequestHeader注释在该比赛中不起作用。
谢谢你的任何提示。
最好的问候,多米尼克
@Configuration
public class AProducer {
@Bean
@Scope(value="request", proxyMode = ScopedProxyMode.TARGET_CLASS)
public User produceUser( @RequestHeader(value="Accept") String acceptType ) {
....
}
}
Run Code Online (Sandbox Code Playgroud) 这是通过简单的主类连接刚刚启动的spring boot项目,它在没有spring-kafka依赖的情况下可以正常工作,但是在添加spring-kafka并spring-kafka-test炸毁之后,github 这里
gradle.build
apply plugin: 'java-library'
repositories {
jcenter()
}
dependencies {
compile group: 'org.springframework.boot', name: 'spring-boot-starter-web', version: '2.1.0.RELEASE'
compile group: 'org.springframework.kafka', name: 'spring-kafka', version: '2.0.5.RELEASE'
testCompile group: 'org.springframework.kafka', name: 'spring-kafka-test', version: '2.0.5.RELEASE'
}
Run Code Online (Sandbox Code Playgroud)
主班
@SpringBootApplication
public class KafkaMain {
public static void main(String[] args) {
SpringApplication.run(KafkaMain.class, args);
}
}
Run Code Online (Sandbox Code Playgroud)
错误
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2018-12-16 19:32:29.347 ERROR 39854 --- [ main] o.s.boot.SpringApplication : Application …Run Code Online (Sandbox Code Playgroud) 我有一个BaseRestControllerRest控制器扩展的类.它有一个我想异步运行的方法.
public abstract class BaseRestController {
...
@Async("someThreadPoolTaskExecutor")
public void someAsyncTask() {
...
}
}
Run Code Online (Sandbox Code Playgroud)
@RestController
public class MyRestController extends BaseRestController {
...
@GetMapping("/some/path")
public SomeEntity getSomething() {
...
this.someAsyncTask();
}
}
Run Code Online (Sandbox Code Playgroud)
我已经启用了Async使用注释,实现了一个获取someThreadPoolTaskExecutor TaskExecutor的方法.如果我把@Async("someThreadPoolTaskExecutor")一个服务的(类注释@Service)方法,它的工作原理,但如果我这样做someAsyncTask()在BaseRestController的代码不会异步运行.使用@Component装饰类也不起作用.
Async的春季指南也没有帮助.在它的演示中,它还演示了与服务类的Async.
虽然在这个过程中,我意识到我想要实现的行为最好委托给服务类,但我很好奇理解为什么上面的行为不起作用.
我正在使用Spring Boot的2.1.0.RELEASE.
我想从application.yml加载服务器属性到Configuration类.我看到很多人已经问过同样的问题,但没有一个对我有用:(请帮我弄清楚我错过了什么
@Configuration
@ConfigurationProperties("demo")
public class Democonfig {
private List<Archive> archive = new ArrayList<>();
public Democonfig(List<Archive> archive) {
this.archive = archive;
}
// Getter and setter
public static class Archive {
private String host;
private String database;
private String port;
public Archive(String host, String database, String port) {
this.host = host;
this.database = database;
this.port = port;
}
// Getters and setters
}
}
Run Code Online (Sandbox Code Playgroud)
application.yml
demo:
archive:
-
host: "localhost"
database: "archive1"
port: "27017"
-
host: "localhost"
database: "archive2"
port: "27017" …Run Code Online (Sandbox Code Playgroud) 我是java和objectMapper的新手。我正在尝试解析json字段,该字段可能是键具有两种类型,它可以是字符串或数组。
例子:
{
"addresses": [],
"full_name": [
"test name_1",
"test name_2"
],
}
Run Code Online (Sandbox Code Playgroud)
要么
{
{
"addresses": [],
"full_name": "test name_3",
}
}
Run Code Online (Sandbox Code Playgroud)
类的例子:
{
"addresses": [],
"full_name": [
"test name_1",
"test name_2"
],
}
Run Code Online (Sandbox Code Playgroud)
我使用objectMapper来反序列化json,当“ full_name”字段具有字符串但到达数组时无法反序列化时,它可以正常工作。
这个想法是,当到达属性时将字符串放入值,但到达数组时,将de数组元素串联为字符串(String.join(“,”,value))
可以在类方法中应用自定义反序列化吗?例如setFullName()(使用lombok.Data)
我在该站点上看到了其他示例,但没有用。
谢谢大家
我有一个非常简单的 json,我试图将其映射到一个对象。
JSON:
[
{
"cust_lpid": "0119b9f7f99ad2161de7b0b",
"cust_uid": "soumavtestflow"
}
]
Run Code Online (Sandbox Code Playgroud)
我的映射器类:
@JsonIgnoreProperties(ignoreUnknown = true)
public class CustomerSegmentRequest {
@JsonProperty("LPID")
String cust_lpid;
@JsonProperty("UserId")
String cust_uid;
public String getCust_lpid() {
return cust_lpid;
}
public void setCust_lpid(String cust_lpid) {
this.cust_lpid = cust_lpid;
}
public String getCust_uid() {
return cust_uid;
}
public void setCust_uid(String cust_uid) {
this.cust_uid = cust_uid;
}
}
Run Code Online (Sandbox Code Playgroud)
当我做一个
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
LPIDCustIDMapper[] custSegResp = objectMapper.readValue(responseBody,CustomerSegmentRequest [].class);
Run Code Online (Sandbox Code Playgroud)
我没有在 custSegResp 中填充任何值。
但是当我删除它时@JsonProperty它就起作用了。我需要 json 属性名称来映射传入请求,因此不想创建单独的映射类。有没有办法达到同样的目的?
在这里,我输入一个字符串,其中用户输入带有扩展名的文件名,我想编写一个程序来给出文件的扩展名,但是当我运行这个程序时,它给出了异常 arrayindexoutofbound 异常,有什么原因吗?
导入java.util.Scanner;
public class Extensionfile {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter file with data type");
String file = sc.next();
String parts[] = file.split(".");
String part1 = parts[1];
System.out.println("The file type is"+part1);
}
}
Run Code Online (Sandbox Code Playgroud) 我是 Spring Boot 的新手。我有一个带有数据的 MYSQL 表“客户”,如下所示: 表中的数据使用 Postman 测试 API 输出时,似乎有几行空的 JSON 输出。
下面是我的代码:
package com.semika.customer;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="customer")
public class Customer {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column(name="first_name")
private String firstName;
@Column(name="last_name")
private String lastName;
@Column(name="address")
private String address;
public Customer() {
super();
}
}
Run Code Online (Sandbox Code Playgroud)
客户资料库
package com.semika.customer;
import org.springframework.data.repository.CrudRepository;
public interface CustomerRepository extends CrudRepository<Customer, Long>{
}
Run Code Online (Sandbox Code Playgroud)
客户服务
package com.semika.customer;
public interface CustomerService …Run Code Online (Sandbox Code Playgroud) 我在 java 8 中使用 apache collections utils 得到的是 null:
if (CollectionUtils.isNotEmpty(reportEnvelopeApps)) {
}
Run Code Online (Sandbox Code Playgroud)
但是如果集合 reportEnvelopeApps 包含一个元素 null,它会意外地工作。所以我必须写这样的代码:
if (reportEnvelopeApps == null) {
return null;
}
reportEnvelopeApps.removeAll(Collections.singleton(null));
if (CollectionUtils.isEmpty(reportEnvelopeApps)) {
return null;
}
Run Code Online (Sandbox Code Playgroud)
避免此类不良代码的更好方法是什么?
java ×10
spring-boot ×5
spring ×3
jackson ×2
date ×1
datetime ×1
java-8 ×1
json ×1
objectmapper ×1
rest ×1
spring-kafka ×1