Rest 如何指定jhipster筛选条件,其中url中的请求参数为字符串,但需要映射到后端中的枚举
我正在处理一个使用jhipster生成的项目。对于实体中的给定字段,值是枚举。在实体上启用筛选后,如何根据枚举值查询实体 api/用户?角色.equals=管理员 无法将类型为“java.lang.String”的属性值转换为属性“role.equals”所需的类型“application.domain.enumeration.Roles”;嵌套异常为org.springframework.core.convert.ConversionFailedException:未能将值“admin”的类型[java.lang.String]转换为类型[application.domain.enumeration.Roles];嵌套异常为java.lang.IllegalArgumentException:无枚举常量application.domain.enumeration.Roles.admin 我认为我需要编写一个自定义规范,但不确定如何继续 用户类型为admin的对象列表 控制器代码:Rest 如何指定jhipster筛选条件,其中url中的请求参数为字符串,但需要映射到后端中的枚举,rest,filter,spring-data-jpa,jhipster,enumeration,Rest,Filter,Spring Data Jpa,Jhipster,Enumeration,我正在处理一个使用jhipster生成的项目。对于实体中的给定字段,值是枚举。在实体上启用筛选后,如何根据枚举值查询实体 api/用户?角色.equals=管理员 无法将类型为“java.lang.String”的属性值转换为属性“role.equals”所需的类型“application.domain.enumeration.Roles”;嵌套异常为org.springframework.core.convert.ConversionFailedException:未能将值“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();
}
}
提供控制器方法和角色的代码enumit还可以帮助说明您试图实现的目标,因为可能有另一种方法可以解决基于实体字段(enum)的issueRest Api URL过滤器;假设一个表用户有列name、email、phone、role;角色具有枚举{student,teacher,parent},i need/api/users?role=parent to work的值。我建议扩展filter类以添加所需的字段,这将允许您以所需的方式筛选实体:)thx..是否尝试在此处更新
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;
}
}
}