他们。我需要你的帮助。我在springboot中创建了一个名为library的项目。现在,我想用jhipster创建相同的项目。我可以看到jhipster主页。(http://localhost:8080正在运行)。另外,我创建了一个用户实体field.It可以在jhipster实体部分或我的数据库中看到。但是我无法通过邮递员或浏览器来达到休息方法。我正在接受“预先认证的入口点被叫。拒绝访问”。你有什么想法吗?
@RestController
@RequestMapping("/api")
public class UsersResource {
public UsersResource() {
System.out.println("3");
}
public static final Logger logger = LoggerFactory.getLogger(UsersResource.class);
private static final String ENTITY_NAME = "users";
@Autowired
UsersService usersService;
/**
* POST /users : Create a new users.
*
* @param users the users to create
* @return the ResponseEntity with status 201 (Created) and with body the new users, 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 < String > createUsers( @RequestBody Users users, UriComponentsBuilder ucBuilder)throws URISyntaxException {
logger.debug("REST request to save Users : {}", users);
if (users.getId() != null) {
return ResponseEntity.badRequest().headers(HeaderUtil.createFailureAlert(ENTITY_NAME, "idexists", "A new users cannot already have an ID")).body(null);
}
usersService.saveUsers(users);
HttpHeaders headers = new HttpHeaders();
headers.setLocation(ucBuilder.path("/api/users/{id}").buildAndExpand(users.getUserId()).toUri());
return new ResponseEntity < String > (headers, HttpStatus.CREATED);
}
/**
* PUT /users : Updates an existing users.
*
* @param users the users to update
* @return the ResponseEntity with status 200 (OK) and with body the updated users,
* or with status 400 (Bad Request) if the users is not valid,
* or with status 500 (Internal Server Error) if the users couldn't be updated
* @throws URISyntaxException if the Location URI syntax is incorrect
*/
@RequestMapping(value = "/users/{id}", method = RequestMethod.PUT)
public ResponseEntity < ? > updateUser( @PathVariable("id")long id, @RequestBody Users user) {
logger.info("Updating User with id {}", id);
Users currentUser = usersService.findById(id);
if (currentUser == null) {
logger.error("Unable to update. User with id {} not found.", id);
return new ResponseEntity(new CustomErrorType("Unable to upate. User with id " + id + " not found."),
HttpStatus.NOT_FOUND);
}
currentUser.setName(user.getName());
currentUser.setSurname(user.getSurname());
currentUser.setEmail(user.getEmail());
usersService.updateUsers(currentUser);
return new ResponseEntity < Users > (currentUser, HttpStatus.OK);
}
/**
* GET /users : get all the users.
*
* @return the ResponseEntity with status 200 (OK) and the list of users in body
*/
@RequestMapping(value = "/users/", method = RequestMethod.GET)
public ResponseEntity < List < Users >> listAllUsers() {
List < Users > users = usersService.findAllUsers();
if (users.isEmpty()) {
return new ResponseEntity(HttpStatus.NO_CONTENT);
// You many decide to return HttpStatus.NOT_FOUND
}
return new ResponseEntity < List < Users >> (users, HttpStatus.OK);
}
/**
* GET /users/:id : get the "id" users.
*
* @param id the id of the users to retrieve
* @return the ResponseEntity with status 200 (OK) and with body the users, or with status 404 (Not Found)
*/
@RequestMapping(value = "/users/{id}", method = RequestMethod.GET)
public ResponseEntity < ? > getUser( @PathVariable("id")long id) {
logger.info("Fetching User with id {}", id);
Users user = usersService.findById(id);
if (user == null) {
logger.error("User with id {} not found.", id);
return new ResponseEntity(new CustomErrorType("User with id " + id
+ " not found"), HttpStatus.NOT_FOUND);
}
return new ResponseEntity < Users > (user, HttpStatus.OK);
}
/**
* DELETE /users/:id : delete the "id" users.
*
* @param id the id of the users to delete
* @return the ResponseEntity with status 200 (OK)
*/
@RequestMapping(value = "/user/{id}", method = RequestMethod.DELETE)
public ResponseEntity < ? > deleteUser( @PathVariable("id")long id) {
logger.info("Fetching & Deleting User with id {}", id);
Users user = usersService.findById(id);
if (user == null) {
logger.error("Unable to delete. User with id {} not found.", id);
return new ResponseEntity(new CustomErrorType("Unable to delete. User with id " + id + " not found."),
HttpStatus.NOT_FOUND);
}
usersService.deleteUsersById(id);
return new ResponseEntity < Users > (HttpStatus.NO_CONTENT);
}
@RequestMapping(value = "/users/", method = RequestMethod.DELETE)
public ResponseEntity < Users > deleteAllUsers() {
logger.info("Deleting All Users");
usersService.deleteAllUsers();
return new ResponseEntity < Users > (HttpStatus.NO_CONTENT);
}
}