rest - 如何指定 jhipster 过滤条件,其中 url 中的请求参数是字符串但需要映射到后端的枚举
问题描述
我正在开发一个使用 jhipster 生成的项目。对于实体中的给定字段,值是枚举。在实体上启用过滤后,如何在枚举值上查询实体
api/users?role.equals=admin
无法将类型“java.lang.String”的属性值转换为属性“role.equals”所需的类型“application.domain.enumeration.Roles”;嵌套异常是 org.springframework.core.convert.ConversionFailedException:无法从类型 [java.lang.String] 转换为值“admin”的类型 [application.domain.enumeration.Roles];嵌套异常是 java.lang.IllegalArgumentException: No enum constant application.domain.enumeration.Roles.admin
我相信我需要编写自定义规范但不知道如何进行
用户类型为 admin 的对象列表
控制器代码:
package org.XXX.web.rest;
import com.codahale.metrics.annotation.Timed;
import io.github.jhipster.web.util.ResponseUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
import java.util.Optional;
/**
* REST controller for managing Users.
*/
@RestController
@RequestMapping("/api")
public class UsersResource {
private final Logger log = LoggerFactory.getLogger(UsersResource.class);
private static final String ENTITY_NAME = "serviceUsers";
private final UsersService usersService;
private final UsersQueryService usersQueryService;
public UsersResource(UsersService usersService, UsersQueryService usersQueryService) {
this.usersService = usersService;
this.usersQueryService = usersQueryService;
}
/**
* POST /users : Create a new users.
*
* @param usersDTO the usersDTO to create
* @return the ResponseEntity with status 201 (Created) and with body the new usersDTO, or with status 400 (Bad Request) if the users has already an ID
* @throws URISyntaxException if the Location URI syntax is incorrect
*/
@PostMapping("/users")
@Timed
public ResponseEntity<UsersDTO> createUsers(@Valid @RequestBody UsersDTO usersDTO) throws URISyntaxException {
log.debug("REST request to save Users : {}", usersDTO);
if (usersDTO.getId() != null) {
throw new BadRequestAlertException("A new users cannot already have an ID", ENTITY_NAME, "idexists");
}
UsersDTO result = usersService.save(usersDTO);
return ResponseEntity.created(new URI("/api/users/" + result.getId()))
.headers(HeaderUtil.createEntityCreationAlert(ENTITY_NAME, result.getId().toString()))
.body(result);
}
/**
* PUT /users : Updates an existing users.
*
* @param usersDTO the usersDTO to update
* @return the ResponseEntity with status 200 (OK) and with body the updated usersDTO,
* or with status 400 (Bad Request) if the usersDTO is not valid,
* or with status 500 (Internal Server Error) if the usersDTO couldn't be updated
* @throws URISyntaxException if the Location URI syntax is incorrect
*/
@PutMapping("/users")
@Timed
public ResponseEntity<UsersDTO> updateUsers(@Valid @RequestBody UsersDTO usersDTO) throws URISyntaxException {
log.debug("REST request to update Users : {}", usersDTO);
if (usersDTO.getId() == null) {
throw new BadRequestAlertException("Invalid id", ENTITY_NAME, "idnull");
}
UsersDTO result = usersService.save(usersDTO);
return ResponseEntity.ok()
.headers(HeaderUtil.createEntityUpdateAlert(ENTITY_NAME, usersDTO.getId().toString()))
.body(result);
}
/**
* GET /users : get all the users.
*
* @param pageable the pagination information
* @param criteria the criterias which the requested entities should match
* @return the ResponseEntity with status 200 (OK) and the list of users in body
*/
@GetMapping("/users")
@Timed
public ResponseEntity<List<UsersDTO>> getAllUsers(UsersCriteria criteria, Pageable pageable) {
log.debug("REST request to get Users by criteria: {}", criteria);
Page<UsersDTO> page = usersQueryService.findByCriteria(criteria, pageable);
HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "/api/users");
return new ResponseEntity<>(page.getContent(), headers, HttpStatus.OK);
}
/**
* GET /users/:id : get the "id" users.
*
* @param id the id of the usersDTO to retrieve
* @return the ResponseEntity with status 200 (OK) and with body the usersDTO, or with status 404 (Not Found)
*/
@GetMapping("/users/{id}")
@Timed
public ResponseEntity<UsersDTO> getUsers(@PathVariable Long id) {
log.debug("REST request to get Users : {}", id);
Optional<UsersDTO> usersDTO = usersService.findOne(id);
return ResponseUtil.wrapOrNotFound(usersDTO);
}
/**
* DELETE /users/:id : delete the "id" users.
*
* @param id the id of the usersDTO to delete
* @return the ResponseEntity with status 200 (OK)
*/
@DeleteMapping("/users/{id}")
@Timed
public ResponseEntity<Void> deleteUsers(@PathVariable Long id) {
log.debug("REST request to delete Users : {}", id);
usersService.delete(id);
return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert(ENTITY_NAME, id.toString())).build();
}
}
枚举代码:
package org.XXX.domain.enumeration;
/**
* The Roles enumeration.
*/
public enum Roles {
NODE, REGIONALADMIN, ADMIN;
public static Roles getRoleSearch(String input) {
switch (input) {
case "NODE":
return NODE;
case "REGIONALADMIN":
return REGIONALADMIN;
case "ADMIN":
return ADMIN;
default:
return null;
}
}
}
解决方案
推荐阅读
- tensorflow - 如何准备 VOTT JSON 数据集以重新训练 COCO SSD Tensorflow api?
- react-native - React Native“每个模块只允许一个默认导出”错误
- python - 检索值以执行简单的 Y=a*x+b 并出现错误
- ruby-on-rails - Ruby检查记录是否存在以及一行中的关联
- angular - Angular 7 router.navigate 重定向后带有消息
- python - SQLite 按每月总销售额分组
- java - 手动设置 HttpStatus 时 @ExceptionHandler 失败
- perl - Strawberry Perl v5.30.0 无法安装 Tcl (Windows 10)
- laravel - Eloquent 构建器实例上不存在属性 [columns]
- mysql - 如何在 WHERE 子句中使用计算值?