当我在 Java 9 中编译 Spring Boot 应用程序时,它会在出现以下几条消息后失败:
package com.fasterxml.jackson.annotation is not visible
(package com.fasterxml.jackson.annotation is declared in the unnamed module, but module com.fasterxml.jackson.annotation does not read it)
Run Code Online (Sandbox Code Playgroud)
有人能告诉我这是怎么回事吗?据我了解,任何不在 Java-9 模块中的 Java 9 之前的代码都将成为未命名模块的一部分,其中会公开任何内容。
我在我的模块中使用它作为注释,如下所示:
@JsonIgnore
public Week getNextWeek()
{
Calendar instance = this.getFirstDay();
instance.set(Calendar.WEEK_OF_YEAR, this.week + 1);
return new Week(instance);
}
Run Code Online (Sandbox Code Playgroud)
那么,如果 com.fasterxml.jackson.annotation 包就是这种情况,为什么错误引用了具有该名称的模块,以及为什么它不读取它会出现问题?
在spring boot应用程序中,我有一个端点,如果存在,该端点将返回带有对象的HTTP 200响应,否则,将返回HTTP 404响应。使用spring-boot-starter-parent 1.5.7我曾经这样做过:
@Autowired private GroceryRepository groceryRepository;
@RequestMapping(value = "/get/{id}", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
public ResponseEntity<Object> get(@PathVariable final Integer id) {
final Grocery grocery = groceryRepository.findOne(id);
if (null == grocery) {
return ResponseEntity
.status(HttpStatus.NOT_FOUND)
.body(MessageUtil.parse(MSG_404_GROCERY, id + ""));
}
return ResponseEntity.ok(grocery);
}
GroceryRepository extends JpaRepository<Grocery, Integer>
Run Code Online (Sandbox Code Playgroud)
在spring-boot-starter-parent 2.0.0中,findOne()已从JpaRepository中消失,并findById()返回Optional。我在如何“移植”上面的代码上有些挣扎。由于它具有意外的返回类型,因此不会构建以下代码:
groceryRepository.findById(id).ifPresent(grocery -> {
return ResponseEntity.ok(grocery);
});
Run Code Online (Sandbox Code Playgroud)
有人可以告诉我返回正确响应的正确方法是什么?
谢谢你的帮助!