我目前正在使用JPA和Kafka的项目.我正在尝试找到一组合并这些操作的良好实践.
在现有代码中,生产者在与jpa相同的事务中使用,但是,根据我的阅读,似乎他们不共享事务.
@PostMapping
@Transactional
public XDto createX(@RequestBody XRequest request) {
Xdto dto = xService.create(request);
kafkaProducer.putToQueue(dto, Type.CREATE);
return dto;
}
Run Code Online (Sandbox Code Playgroud)
其中kafka生产者的定义如下:
public class KafkaProducer {
@Autowired
private KafkaTemplate<String, Type> template;
public void putToQueue(Dto dto, Type eventType) {
template.send("event", new Event(dto, eventType));
}
}
Run Code Online (Sandbox Code Playgroud)
这是组合jpa和kafka的有效用例,是否正确定义了事务边界?
我想知道是否有可能在运行时检索该类来自的jar的版本号?
我知道有可能找到该类来自的jar:
MyClass.class.getProtectionDomain().getCodeSource().getLocation().getPath();
Run Code Online (Sandbox Code Playgroud)
但是版本怎么样?
(假设它不在文件名:))
我有3个独立的弹簧网应用程序
B和C公开REST控制器以上载文件
所以流程将是A-> B-> C.
我的问题是 - 是否有可能以这样的方式设置B,以便B不会将整个文件存储在内存中,但会读取传入的流并将其转发到C?
我设法做的是: A
public void sendFileFromA() throws FileNotFoundException {
final InputStream fis = new FileInputStream(new File("someFile"));
final RequestCallback requestCallback = new RequestCallback() {
@Override
public void doWithRequest(final ClientHttpRequest request) throws IOException {
request.getHeaders().add("Content-type", "application/octet-stream");
IOUtils.copy(fis, request.getBody());
}
};
final RestTemplate restTemplate = new RestTemplate();
SimpleClientHttpRequestFactory requestFactory = …Run Code Online (Sandbox Code Playgroud) 有没有办法用curl复制本地文件,我需要它作为cp命令的替代方案.
这有点奇怪,但我正在开发一个cp不可用的环境.
在JUnit/Mockito中,我们有2个非常有用的注释:@Mock和@InjectMocks.
在我的新项目中,我开始使用带有spock的groovy进行测试,我想知道是否有替代提到的注释?
我正在进行代码审查,我注意到这样的代码:
@Entity
@Table(name = "SOME_TABLE")
public class SomeReportClass {
@Column(name = "REPORT_NUMBER", length = 6, nullable = false)
private String reportNumber;
.....
public String getReportNumber() {
return reportNumber;
}
public void setReportNumber(String reportNumber) {
this.reportNumber = StringUtils.trimToNull(reportNumber);
}
Run Code Online (Sandbox Code Playgroud)
}
每当我看到修剪器内部的修剪时,我觉得它不是最清晰的解决方案 - 这个问题的一般做法是什么?
我需要使用的库以下列方式读取系统属性:
System.getProperty("library.system.property")
Run Code Online (Sandbox Code Playgroud)
有没有办法在启动应用程序时将这样的属性传递给spring引导,还是我需要在系统中设置它?
我正在寻找一种从休息模板中获取打开的输入流的方法 - 我试图使用ResponseExtractor,但是在返回之前流已经关闭,如下所示:
https://jira.spring.io/browse/SPR-7357
"请注意,您不能简单地从提取器返回InputStream,因为在execute方法返回时,底层连接和流已经关闭"
我希望有一种方法,我不必直接在其余模板中写入我的输出流.
我对JAXB有一个小问题,但是不幸的是我找不到答案。
我有一个Customer类,其中有两个字段name和city,映射是使用注释完成的,并且两个字段都标记为必填字段,并且不可为空。
@XmlRootElement(name = "customer")
public class Customer {
enum City {
PARIS, LONDON, WARSAW
}
@XmlElement(name = "name", required = true, nillable = false)
public String name;
@XmlElement(name = "city", required = true, nillable = false)
public City city;
@Override
public String toString(){
return String.format("Name %s, city %s", name, city);
}
}
Run Code Online (Sandbox Code Playgroud)
但是,当我提交这样的XML文件时:
<customer>
<city>UNKNOWN</city>
</customer>
Run Code Online (Sandbox Code Playgroud)
我将收到两个字段都设置为null的Customer实例。
是否应该没有验证例外,或者我在映射中缺少某些内容?
为了解组,我使用:
JAXBContext jaxbContext = JAXBContext.newInstance(Customer.class);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
Customer customer = (Customer) unmarshaller.unmarshal(in);
Run Code Online (Sandbox Code Playgroud) 这似乎是一个简单的问题,但我找不到答案。
在 spring-boot 中,我有一个返回 json 的 Controller 方法:
@GetMapping
public ResponseEntity<Stream<MyObject>> get() {
return ResponseEntity
.ok()
.contentType(MEDIA_JSON)
.body(service.stream());
}
Run Code Online (Sandbox Code Playgroud)
这是正确地流式传输给用户(以块为单位)还是在返回给用户之前将整个流加载到内存中?
我有一个 Spring 项目,我使用 hibernate 验证器并有一个 Junit 测试类,它使用以下代码:
Set<ConstraintViolation<Rule>> constraintViolations = validator.validateProperty(myObject, "query");
assertEquals(1, constraintViolations.size());
Run Code Online (Sandbox Code Playgroud)
但是,我发现这不是一个好的测试方法。我有一个NotBlank注释,该测试类检查它是否有效。但是,如果我放置任何其他违反的约束,则将constraintViolations.size()是2。
我的问题是:如何检查是否NotBlank违规?
有人可以解释我为什么以下代码:
Splitter.on("\n").trimResults(CharMatcher.is('|')).trimResults().split("|a\nb|\nc|")
Run Code Online (Sandbox Code Playgroud)
回报
[|a,b|,c|]
Run Code Online (Sandbox Code Playgroud)
代替
[a,b,c]
Run Code Online (Sandbox Code Playgroud) java ×11
spring ×4
spring-boot ×3
spring-mvc ×3
apache-kafka ×1
curl ×1
groovy ×1
guava ×1
jar ×1
jaxb ×1
jpa ×1
junit ×1
linux ×1
mockito ×1
reflection ×1
rest ×1
resttemplate ×1
spock ×1
unix ×1
validation ×1
xml ×1