我目前正在用Java学习micronaut,并在此site中做一个例子。但是一切似乎都没有我预期的那样
以下是一些代码:
CrudRepository.java
public interface CrudRepository<T, ID extends Serializable> {
Optional<T> findById(@NotNull ID id);
T save(@NotBlank String name);
void deleteById(@NotNull Long id);
List<T> findAll(@NotNull SortingAndOrderArguments args);
int update(@NotNull ID id, @NotBlank String name);
}
GenreRepository(实际上与GenreRepositoryImpl相同,就像spring webapp中的其他约定)
public class GenreRepository implements CrudRepository<Genre, Long> {
@PersistenceContext
private EntityManager entityManager;
private final AppConfig appConfig;
public GenreRepository(@CurrentSession EntityManager entityManager,
AppConfig appConfig) {
this.entityManager = entityManager;
this.appConfig = appConfig;
}
@Override
@Transactional(readOnly = true)
public Optional<Genre> findById(@NotNull Long id) {
return Optional.ofNullable(entityManager.find(Genre.class, id));
}
@Override
@Transactional
public Genre save(@NotBlank String name) {
Genre genre = new Genre(name);
entityManager.persist(genre);
return genre;
}
@Override
@Transactional
public void deleteById(@NotNull Long id) {
findById(id).ifPresent(genre -> entityManager.remove(genre));
}
private final static List<String> VALID_PROPERTY_NAMES = Arrays.asList("id", "name");
@Transactional(readOnly = true)
public List<Genre> findAll(@NotNull SortingAndOrderArguments args) {
String qlString = "SELECT g FROM Genre as g";
if (args.getOrder().isPresent() && args.getSort().isPresent() && VALID_PROPERTY_NAMES.contains(args.getSort().get())) {
qlString += " ORDER BY g." + args.getSort().get() + " " + args.getOrder().get().toLowerCase();
}
TypedQuery<Genre> query = entityManager.createQuery(qlString, Genre.class);
query.setMaxResults(args.getMax().orElseGet(appConfig::getMax));
args.getOffset().ifPresent(query::setFirstResult);
return query.getResultList();
}
@Override
@Transactional
public int update(@NotNull Long id, @NotBlank String name) {
return entityManager.createQuery("UPDATE Genre g SET name = :name where id = :id")
.setParameter("name", name)
.setParameter("id", id)
.executeUpdate();
}
}
GenreSaveDTO.java
public class GenreSaveDTO {
@NotBlank
private String name;
public GenreSaveDTO() {}
public GenreSaveDTO(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
GenreController.java
@Validated
@Controller("/genres")
public class GenreController {
protected final GenreRepository genreRepository;
public GenreController(GenreRepository genreRepository) {
this.genreRepository = genreRepository;
}
@Get("/{id}")
public Genre show(Long id) {
return genreRepository
.findById(id)
.orElseThrow(null);
}
@Put("/")
public HttpResponse update(@Body @Valid GenreUpdateDTO dto) {
int numberOfEntitiesUpdated = genreRepository.update(dto.getId(), dto.getName());
return HttpResponse
.noContent()
.header(HttpHeaders.LOCATION, location(dto.getId()).getPath());
}
@Get(value = "/list{?args*}")
public List<Genre> list(@Valid SortingAndOrderArguments args) {
return genreRepository.findAll(args);
}
@Post("/")
public HttpResponse<Genre> save(@Body @Valid GenreSaveDTO dto) {
Genre genre = genreRepository.save(dto.getName());
return HttpResponse
.created(genre)
.headers(headers -> headers.location(location(genre.getId())));
}
@Delete("/{id}")
public HttpResponse delete(Long id) {
genreRepository.deleteById(id);
return HttpResponse.noContent();
}
protected URI location(Long id) {
return URI.create("/genres/" + id);
}
protected URI location(Genre genre) {
return location(genre.getId());
}
}
当我运行服务器并通过使用curl来测试api时:
curl -X "POST" "http://localhost:8080/genres" \
-H 'Content-Type: application/json; charset=utf-8' \
-d $'{
"name": "music"
}'
但是控制台记录了这种错误:
21:10:42.112 [nioEventLoopGroup-1-5] ERROR i.m.h.s.n.c.UnsatisfiedRouteHandler - POST /genres (Bad Request): Required argument [GenreSaveDTO dto] not specified
预先感谢