@Override public void propertyField(JFieldVar field, JDefinedClass clazz, String propertyName, JsonNode propertyNode) { field.annotate(JsonProperty.class).param("value", propertyName); if (field.type().erasure().equals(field.type().owner().ref(Set.class))) { field.annotate(JsonDeserialize.class).param("as", LinkedHashSet.class); } if (propertyNode.has("javaJsonView")) { field.annotate(JsonView.class).param( "value", field.type().owner().ref(propertyNode.get("javaJsonView").asText())); } if (propertyNode.has("description")) { field.annotate(JsonPropertyDescription.class).param("value", propertyNode.get("description").asText()); } }
@GetMapping(value = "/{slug}") @JsonView(View.Public.class) public ResponseEntity<Post> getPost(@PathVariable("slug") String slug) { log.debug("get postsinfo by slug @" + slug); Post post = this.postRepository.findBySlug(slug).orElseThrow( () -> { return new PostNotFoundException(slug); } ); log.debug("get post @" + post); return new ResponseEntity<>(post, HttpStatus.OK); }
@JsonView(JsonViews.Basique.class) @RequestMapping(value = "/api/sprint/delete/{id}", method = RequestMethod.DELETE, produces = "application/json") public ResponseEntity<Sprint> deleteSprint(@PathVariable("id") Long id) { LOGGER.debug("ApiController - deleteSprint"); Sprint sprint = null; if(sprintService.exists(id)) { sprint = sprintService.findOneById(id); sprintService.deleteOneById(sprint.getId()); return new ResponseEntity<Sprint>(sprint, HttpStatus.ACCEPTED); } return new ResponseEntity<Sprint>(sprint, HttpStatus.NOT_FOUND); }
@JsonView(JsonViews.Basique.class) @RequestMapping(value = "/api/sprint/{idSprint}/story/{idStory}", method = RequestMethod.POST, produces = "application/json") public ResponseEntity<Sprint> linkSprintIdWithStoryId(@PathVariable Long idSprint, @PathVariable Long idStory) { LOGGER.debug("ApiController - linkSprintIdWithStoryId"); Story story = storyService.findOneByIdWithAll(idStory); Sprint sprint = sprintService.findOneByIdWithAll(idSprint); if(null==sprint ||null ==story) { return new ResponseEntity<Sprint>(sprint, HttpStatus.NOT_FOUND); } story.setStorySprint(sprint); story = storyService.save(story); sprint = sprintService.findOneByIdWithAll(idSprint); return new ResponseEntity<Sprint>(sprint, HttpStatus.CREATED); }
@JsonView(JsonViews.Project.class) @ResponseStatus(HttpStatus.OK) @RequestMapping(value = "/api/project/details", method = {RequestMethod.GET, RequestMethod.POST}, produces = "application/json") public List<Project> showProjectsDetails() { LOGGER.debug("ApiController - showProjectsDetails"); return projectService.findAllWithAll(); }
@JsonView(JsonViews.Basique.class) @RequestMapping(value = "/api/user/add", method = RequestMethod.POST, produces = "application/json") public ResponseEntity<User> sendUser(@RequestBody User user) { LOGGER.debug("ApiController - sendUser"); if(!userService.exists(user.getEmail())) { userService.save(user); LOGGER.debug("ApiController - sendUser - User créé"); return new ResponseEntity<User>(user, HttpStatus.CREATED); } else { LOGGER.debug("ApiController - sendUser - User déjà existant"); return new ResponseEntity<User>(user, HttpStatus.CONFLICT); } }
@JsonView(JsonViews.Basique.class) @RequestMapping(value = "/api/story/update/{idStory}", method = RequestMethod.POST, produces = "application/json") public ResponseEntity<Story> updateStory(@PathVariable Long idStory, @RequestBody Story story, @RequestParam Long idSprint) { LOGGER.debug("ApiController - updateStory"); if(idSprint != null) { Sprint sprint = sprintService.findOneById(idSprint); if(null==sprint) { LOGGER.debug("ApiController - updateStory - Sprint inexistant"); return new ResponseEntity<Story>(story, HttpStatus.NOT_FOUND); } story.setStorySprint(sprint); } storyService.updateStory(idStory, story); LOGGER.debug("ApiController - updateStory - Story maj"); return new ResponseEntity<Story>(story, HttpStatus.ACCEPTED); }
@JsonView(JsonViews.Basique.class) @RequestMapping(value = "/api/story/add/sprint/{idSprint}", method = RequestMethod.POST, produces = "application/json") public ResponseEntity<Story> sendStoryWithSprintId(@PathVariable Long idSprint, @RequestBody Story story) { LOGGER.debug("ApiController - sendStoryWithSprintId"); Sprint sprint = sprintService.findOneById(idSprint); if(null==sprint) { LOGGER.debug("ApiController - sendStoryWithSprintId - Sprint inexistant"); return new ResponseEntity<Story>(story, HttpStatus.NOT_FOUND); } if(story.getStatus()==null) story.setStatus(StoryStatus.TODO); story = storyService.save(story); story.setStorySprint(sprint); story = storyService.save(story); LOGGER.debug("ApiController - sendStoryWithSprintId - Story créé"); return new ResponseEntity<Story>(story, HttpStatus.CREATED); }
@JsonView(JsonViews.Basique.class) @RequestMapping(value = "/api/task/delete/{id}", method = RequestMethod.DELETE, produces = "application/json") public ResponseEntity<Task> deleteTask(@PathVariable("id") Long id) { LOGGER.debug("ApiController - deleteTask"); Task task = null; if(taskService.exists(id)) { task = taskService.findOneById(id); taskService.deleteOneById(task.getId()); return new ResponseEntity<Task>(task, HttpStatus.ACCEPTED); } return new ResponseEntity<Task>(task, HttpStatus.NOT_FOUND); }
@JsonView(Views.Public.class) @RequestMapping(value = "/answer/user/{name}", method = RequestMethod.GET) public ResponseEntity<?> getAnswersByUser(@PathVariable("name") String name) { User user = userService.getByUsername(name); if (user == null) { return new ResponseEntity(HttpStatus.NO_CONTENT); } List<Answer> answers = answerService.getByUser(user); if (answers.isEmpty()) { return new ResponseEntity(HttpStatus.NO_CONTENT); } return new ResponseEntity<List<Answer>>(answers, HttpStatus.OK); }
@JsonView(Views.Public.class) @RequestMapping(value = "/questions/user/{name}", method = RequestMethod.GET) public ResponseEntity<?> getQuestionsByUser(@PathVariable("name") String name) { User user = userService.getByUsername(name); if (user == null) { return new ResponseEntity(HttpStatus.NO_CONTENT); } List<Question> questions = questionService.getByUser(user); if (questions.isEmpty()) { return new ResponseEntity(HttpStatus.NO_CONTENT); } return new ResponseEntity<List<Question>>(questions, HttpStatus.OK); }
@JsonView(Views.Public.class) @RequestMapping(value = "/questions/tag/{name}", method = RequestMethod.GET) public ResponseEntity<?> getQuestionsByTag(@PathVariable("name") String name) { Tag tag = tagService.getByName(name); if (tag == null) { return new ResponseEntity(HttpStatus.NO_CONTENT); } List<Question> questions = questionService.getByTag(tag); if (questions.isEmpty()) { return new ResponseEntity(HttpStatus.NO_CONTENT); } return new ResponseEntity<List<Question>>(questions, HttpStatus.OK); }
@JsonView(Views.Public.class) @RequestMapping(value = "/login", method = RequestMethod.POST) public ResponseEntity<?> login(@RequestBody LoginModel data) { User user = userService.getByUsername(data.getUsername()); if (user == null) { return new ResponseEntity(new LoginResponseBody(false, null, "User with that name isn't exist"), HttpStatus.OK); } if (!Objects.equals(user.getPassword(), MD5.getHash(data.getPassword()))) { return new ResponseEntity(new LoginResponseBody(false, null, "wrong_password"), HttpStatus.OK); } String token = Jwts.builder() .setSubject(data.getUsername()) .signWith(SignatureAlgorithm.HS512, key) .compact(); return new ResponseEntity(new LoginResponseBody(true, token), HttpStatus.OK); }
@JsonView(Views.Public.class) @RequestMapping(value = "/register", method = RequestMethod.POST) public ResponseEntity<?> register(@RequestBody LoginModel data) { User user = userService.getByUsername(data.getUsername()); if (user != null) { return new ResponseEntity(new LoginResponseBody(false, null, "User with that name has already existed"), HttpStatus.OK); } User newUser = new User(data.getUsername(), MD5.getHash(data.getPassword()), new Date(), "active", 0); userService.addUser(newUser); String token = Jwts.builder() .setSubject(newUser.getUsername()) .signWith(SignatureAlgorithm.HS512, key) .compact(); return new ResponseEntity(new LoginResponseBody(true, token), HttpStatus.OK); }
@POST @Produces("application/json") @JsonView(RestrictedViews.Member.class) @MinRole(Role.ADMIN) @Path("/admin-create-user") public Object adminCreateUser(@ObjectParam User newUser) { if (empty(newUser.getEmail())) { newUser.setEmail(newUser.getUsername()); } else if (empty(newUser.getUsername())) { newUser.setUsername(newUser.getEmail()); } IUser user = SafeMerger.with() .nonEmpty("email", "username", "displayName", "role") .optional("familyName", "givenName") .merge(newUser); IUser existing = UserController.instance().forEmail(user.getEmail()); if (existing != null) { throw new ClientException("A user with that email address already exists."); } user.setApproved(true); UserController.instance().createUser(user); return user; }
@GET @Path("/users-table") @MinRole(Role.ADMIN) @Produces("application/json") @JsonView(RestrictedViews.Owner.class) public Pager usersTable(@QueryParam("page") Integer page, @QueryParam("withDeleted") Boolean withDeleted) { Map<String, Object> ctx = map(); if (page == null || page < 1) { page = 1; } withDeleted = or(withDeleted, false); FilterChain chain = UserController.instance().filterChain(); if (withDeleted) { chain = chain.includeDeleted(); } Pager pager = chain.sort("email", "ASC").pager(page, 100); return pager; }
/** * Determine a Jackson serialization view based on the given conversion hint. * @param conversionHint the conversion hint Object as passed into the * converter for the current conversion attempt * @return the serialization view class, or {@code null} if none * @since 4.2 */ protected Class<?> getSerializationView(Object conversionHint) { if (conversionHint instanceof MethodParameter) { MethodParameter param = (MethodParameter) conversionHint; JsonView annotation = (param.getParameterIndex() >= 0 ? param.getParameterAnnotation(JsonView.class) : param.getMethodAnnotation(JsonView.class)); if (annotation != null) { return extractViewClass(annotation, conversionHint); } } else if (conversionHint instanceof JsonView) { return extractViewClass((JsonView) conversionHint, conversionHint); } else if (conversionHint instanceof Class) { return (Class) conversionHint; } // No JSON view specified... return null; }
@Test public void renderSimpleBeanWithJsonView() throws Exception { Object bean = new TestBeanSimple(); Map<String, Object> model = new HashMap<String, Object>(); model.put("bindingResult", mock(BindingResult.class, "binding_result")); model.put("foo", bean); model.put(JsonView.class.getName(), MyJacksonView1.class); view.setUpdateContentLength(true); view.render(model, request, response); String content = response.getContentAsString(); assertTrue(content.length() > 0); assertEquals(content.length(), response.getContentLength()); assertTrue(content.contains("foo")); assertFalse(content.contains("boo")); assertFalse(content.contains(JsonView.class.getName())); }
@JsonView(ModelBase.API.class) @RequestMapping(method=RequestMethod.POST) public ResponseEntity<?> create(@AuthenticationPrincipal User user, @Valid @ModelAttribute TaskForm form, Errors errors) { Task task; if(errors.hasErrors()) { return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(errors.toString()); } try { task = new Task(); task.setPriority(Task.TaskPriority.NORMAL); task.setBelongsTo(user); task.setBelongsToName(user.getName()); task.setBelongsToEmail(user.getEmail()); taskService.save(form.push(task)); } catch (DataAccessException ex) { return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(ex); } return ResponseEntity.ok(task); }
@JsonView(ModelBase.API.class) @RequestMapping(value="/{id:[0-9]+}", method=RequestMethod.POST) public ResponseEntity<?> update(@PathVariable("id") Long id, @Valid @ModelAttribute TaskForm form, Errors errors) { Task task; if(errors.hasErrors()) { return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(errors.toString()); } try { task = taskService.findOne(id); taskService.save(form.push(task)); } catch (DataAccessException ex) { return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(ex); } return ResponseEntity.ok(task); }
@ApiOperation(value = "Primary Health Check", notes = "The response JSON will contain a message and the results of checking the health of " + "primary dependencies, although stack traces will be excluded.", response = HealthDto.class) @ApiResponses(value = { @ApiResponse(code = 503, message = "unhealthy", response = HealthDto.class), @ApiResponse(code = 200, message = "healthy", response = HealthDto.class)}) @GET @Produces(MediaType.APPLICATION_JSON) @JsonView(HealthJsonViews.Primary.class) Response getPrimaryHealthCheck();
@ApiOperation(value = "Availability Check for Dependency", notes = "Invokes the basic availability check on the given dependency. " + "The response code will match the code returned by the dependency, and will be " + "omitted from the JSON.", response = HealthDependencyDto.class) @ApiResponses(value = { @ApiResponse(code = 503, message = "unhealthy", response = HealthDependencyDto.class), @ApiResponse(code = 200, message = "healthy", response = HealthDependencyDto.class)}) @GET @Produces(MediaType.APPLICATION_JSON) @JsonView(HealthJsonViews.Dependency.class) @Path("/{name}") Response getDependencyAvailabilityCheck(@PathParam("name") String name);
@ApiOperation(value = "Basic Availability Check", notes = "Always returns 200. The JSON will only include the message field.", response = HealthDto.class) @ApiResponses(value = { @ApiResponse(code = 503, message = "unhealthy"), @ApiResponse(code = 200, message = "healthy")}) @GET @Produces(MediaType.APPLICATION_JSON) @JsonView(HealthJsonViews.Availability.class) HealthDto getBasicAvailabilityCheck();
@ApiOperation(value = "Diagnostic Health Check", notes = "The response JSON will contain all available diagnostic details and the results of " + "checking the health of all dependencies.", response = HealthDto.class) @ApiResponses(value = { @ApiResponse(code = 503, message = "unhealthy", response = HealthDto.class), @ApiResponse(code = 200, message = "healthy", response = HealthDto.class)}) @GET @Produces(MediaType.APPLICATION_JSON) @JsonView(HealthJsonViews.Diagnostic.class) Response getDiagnosticHealthCheck();
@JsonView(JsonViews.Basique.class) @ResponseStatus(HttpStatus.OK) @RequestMapping(value = "/api/sprint", method = {RequestMethod.GET, RequestMethod.POST}, produces = "application/json") public List<Sprint> showSprints() { LOGGER.debug("ApiController - showSprints"); return sprintService.findAll(); }
@JsonView(JsonViews.Sprint.class) @ResponseStatus(HttpStatus.OK) @RequestMapping(value = "/api/sprint/details", method = {RequestMethod.GET, RequestMethod.POST}, produces = "application/json") public List<Sprint> showSprintsDetails() { LOGGER.debug("ApiController - showSprintsDetails"); return sprintService.findAllWithAll(); }
@JsonView(JsonViews.Basique.class) @ResponseStatus(HttpStatus.OK) @RequestMapping(value = "/api/sprint/{id}", method = {RequestMethod.GET, RequestMethod.POST}, produces = "application/json") public ResponseEntity<Sprint> showSprint(@PathVariable Long id) { LOGGER.debug("ApiController - showSprint"); Sprint sprint = null; if(sprintService.exists(id)) { sprint = sprintService.findOneById(id); return new ResponseEntity<Sprint>(sprint, HttpStatus.OK); } return new ResponseEntity<Sprint>(sprint, HttpStatus.NOT_FOUND); }
@JsonView(JsonViews.Sprint.class) @ResponseStatus(HttpStatus.OK) @RequestMapping(value = "/api/sprint/{id}/details", method = {RequestMethod.GET, RequestMethod.POST}, produces = "application/json") public ResponseEntity<Sprint> showSprintWithAll(@PathVariable Long id) { LOGGER.debug("ApiController - showSprint"); Sprint sprint = null; if(sprintService.exists(id)) { sprint = sprintService.findOneByIdWithAll(id); return new ResponseEntity<Sprint>(sprint, HttpStatus.OK); } return new ResponseEntity<Sprint>(sprint, HttpStatus.NOT_FOUND); }
@JsonView(JsonViews.Basique.class) @RequestMapping(value = "/api/sprint/add", method = RequestMethod.POST, produces = "application/json") public ResponseEntity<Sprint> sendSprint(@RequestBody Sprint sprint) { LOGGER.debug("ApiController - sendSprint"); sprint = sprintService.save(sprint); LOGGER.debug("ApiController - sendSprint - Sprint créé"); return new ResponseEntity<Sprint>(sprint, HttpStatus.CREATED); }
@JsonView(JsonViews.Basique.class) @RequestMapping(value = "/api/sprint/add/project/{idProject}", method = RequestMethod.POST, produces = "application/json") public ResponseEntity<Sprint> sendSprintLinkedToProject(@RequestBody Sprint sprint, @PathVariable Long idProject) { LOGGER.debug("ApiController - sendSprintLinkedToProject"); Project project = projectService.findOneById(idProject); if(project==null) return new ResponseEntity<Sprint>(sprint, HttpStatus.NOT_FOUND); sprint.setSprintProject(project); sprintService.save(sprint); LOGGER.debug("ApiController - sendSprintLinkedToProject - Sprint créé et lié à un projet"); return new ResponseEntity<Sprint>(sprint, HttpStatus.CREATED); }
@JsonView(JsonViews.Basique.class) @RequestMapping(value = "/api/sprint/update/{idSprint}", method = RequestMethod.POST, produces = "application/json") public ResponseEntity<Sprint> updateSprint(@PathVariable Long idSprint, @RequestBody Sprint sprint) { LOGGER.debug("ApiController - updateSprint"); sprintService.updateSprint(idSprint, sprint); LOGGER.debug("ApiController - updateSprint - Project maj"); return new ResponseEntity<Sprint>(sprint, HttpStatus.ACCEPTED); }
@JsonView(JsonViews.Basique.class) @ResponseStatus(HttpStatus.OK) @RequestMapping(value = "/api/sprint/project/{idProject}", method = {RequestMethod.GET, RequestMethod.POST}, produces = "application/json") public Set<Sprint> showSprintsAssociatedToThisProject(@PathVariable Long idProject) { LOGGER.debug("ApiController - showSprintsAssociatedToThisProject"); Set<Sprint> sprints = sprintService.findBySprintProject(idProject); return sprints; }
@JsonView(JsonViews.Basique.class) @ResponseStatus(HttpStatus.OK) @RequestMapping(value = "/api/project", method = {RequestMethod.GET, RequestMethod.POST}, produces = "application/json") public List<Project> showProjects() { LOGGER.debug("ApiController - showProjects"); return projectService.findAll(); }
@JsonView(JsonViews.Basique.class) @ResponseStatus(HttpStatus.OK) @RequestMapping(value = "/api/project/{id}", method = {RequestMethod.GET, RequestMethod.POST}, produces = "application/json") public ResponseEntity<Project> showProject(@PathVariable Long id) { LOGGER.debug("ApiController - showProject"); Project project = null; if(projectService.exists(id)) { project = projectService.findOneById(id); return new ResponseEntity<Project>(project, HttpStatus.OK); } return new ResponseEntity<Project>(project, HttpStatus.NOT_FOUND); }
@JsonView(JsonViews.Project.class) @ResponseStatus(HttpStatus.OK) @RequestMapping(value = "/api/project/{id}/details", method = {RequestMethod.GET, RequestMethod.POST}, produces = "application/json") public ResponseEntity<Project> showProjectWithAll(@PathVariable Long id) { LOGGER.debug("ApiController - showProjectWithAll"); Project project = null; if(projectService.exists(id)) { project = projectService.findOneByIdWithAll(id); return new ResponseEntity<Project>(project, HttpStatus.OK); } return new ResponseEntity<Project>(project, HttpStatus.NOT_FOUND); }
@JsonView(JsonViews.Basique.class) @RequestMapping(value = "/api/project/add", method = RequestMethod.POST, produces = "application/json") public ResponseEntity<Project> sendProject(@RequestBody Project project) { LOGGER.debug("ApiController - sendProject"); project = projectService.save(project); LOGGER.debug("ApiController - sendProject - Projet créé"); return new ResponseEntity<Project>(project, HttpStatus.CREATED); }
@JsonView(JsonViews.Basique.class) @RequestMapping(value = "/api/project/update/{idProject}", method = RequestMethod.POST, produces = "application/json") public ResponseEntity<Project> updateProject(@PathVariable Long idProject, @RequestBody Project project) { LOGGER.debug("ApiController - updateProject"); projectService.updateProject(idProject, project); LOGGER.debug("ApiController - updateProject - Project maj"); return new ResponseEntity<Project>(project, HttpStatus.ACCEPTED); }
@JsonView(JsonViews.Basique.class) @RequestMapping(value = "/api/project/delete/{id}", method = RequestMethod.DELETE, produces = "application/json") public ResponseEntity<Project> deleteProject(@PathVariable("id") Long id) { LOGGER.debug("ApiController - deleteProject"); Project project = null; if(projectService.exists(id)) { project = projectService.findOneById(id); projectService.deleteOneById(id); return new ResponseEntity<Project>(project, HttpStatus.ACCEPTED); } return new ResponseEntity<Project>(project, HttpStatus.NOT_FOUND); }