@Test public void canNotCancelRaceIfNotStarted() { when() .post(CurrentRaceController.CANCEL_RACE_URL) .then() .statusCode(HttpStatus.NOT_FOUND.value()); }
@ResponseBody @RequestMapping(value = "modify", method = RequestMethod.POST) public String modify(HttpServletRequest request, HttpServletResponse response) { JSONObject json = FormRequestUtil.parseData(request); Object _id = json.get("id"); if (_id == null) { response.setStatus(HttpStatus.BAD_REQUEST.value()); return "媒体Id不能为空"; } int id = Integer.parseInt(_id.toString()); Media media = service.getById(id); if (media != null) { String desc = (String) json.get("desc"); String logourl = (String) json.get("logourl"); String siteurl = (String) json.get("siteurl"); if (StringUtils.isNotBlank(desc)) { media.setDesc(desc); } if (StringUtils.isNotBlank(logourl)) { media.setLogourl(logourl); } if (StringUtils.isNotBlank(siteurl)) { media.setSiteurl(siteurl); } return media.toJson().toJSONString(); } else { response.setStatus(HttpStatus.NOT_FOUND.value()); return "媒体[id=" + _id + "]不存在"; } }
@RequestMapping(value = "/{id:.+}", method = GET) public void getFile(@PathVariable("id") String id, HttpServletResponse response) throws IOException { FileMeta fileMeta = dataService.findOne(FileMeta.ENTITY_NAME, id, FileMeta.class); if (fileMeta == null) { response.setStatus(HttpStatus.NOT_FOUND.value()); } else { java.io.File fileStoreFile = fileStore.getFile(fileMeta.getFilename()); // if file meta data exists for this file String outputFilename = fileMeta.getFilename(); String contentType = fileMeta.getContentType(); if (contentType != null) { response.setContentType(contentType); } Long size = fileMeta.getSize(); if (size != null) { response.setContentLength(size.intValue()); } response.setHeader( "Content-Disposition", "attachment; filename=" + outputFilename.replace(" ", "_")); InputStream is = new FileInputStream(fileStoreFile); try { FileCopyUtils.copy(is, response.getOutputStream()); } finally { is.close(); } } }
@Test public void shouldReturnNotFoundWhenDeletingARecordThatDoesNotExist() { when(mockService.get(argThat(isString(TEST_ID)))).thenReturn(null); Response response = recordResource.deleteRecord(TEST_ID); assertThat(response.getStatus(), is(HttpStatus.NOT_FOUND.value())); }
/** * Returned response code should be 404 if data was not found. * * @param ex Caused exception. * @return Error message. */ @ExceptionHandler @ResponseStatus(HttpStatus.NOT_FOUND) @ResponseBody public ErrorElementType handleException(final NotFoundException ex) { LOGGER.info("Advice logging(Not found): " + ex.getMessage()); ErrorElementType element = new ErrorElementType(); element.setErrorcode(BigInteger.valueOf(HttpStatus.NOT_FOUND.value())); element.setMessage("No data was found."); return element; }
private void writeResponse(GridFSDBFile file, HttpServletResponse response) throws IOException { if (file != null) { byte[] data = IOUtils.toByteArray(file.getInputStream()); response.setContentType(file.getContentType()); response.setContentLength((int) file.getLength()); response.getOutputStream().write(data); response.getOutputStream().flush(); } else { response.setStatus(HttpStatus.NOT_FOUND.value()); } }
@Test public void shouldReturnNotFoundWhenGettingARecordThatDoesNotExist() { when(mockService.get(argThat(isString(TEST_ID)))).thenReturn(null); Response response = recordResource.getRecord(TEST_ID); RecordPayload recordPayload = (RecordPayload) response.getEntity(); assertThat(response.getStatus(), is(HttpStatus.NOT_FOUND.value())); assertThat(recordPayload, is(nullValue())); }
@RequestMapping(value = "/{id}", method = RequestMethod.GET) public ResponseEntity<Map<String, Object>> deleteMemo(@PathVariable("id") int id) { Map<String, Object> map = new HashMap<String, Object>(); if (memoDao.deleteMemo(id)) { map.put("MESSAGE", "MEMO HAS BEEN DELETED."); map.put("STATUS", HttpStatus.FOUND.value()); return new ResponseEntity<Map<String, Object>>(map, HttpStatus.OK); } else { map.put("MESSAGE", "MEMO HAS NOT BEEN DELETED."); map.put("STATUS", HttpStatus.NOT_FOUND.value()); return new ResponseEntity<Map<String, Object>>(map, HttpStatus.NOT_FOUND); } }
@Test public void notFoundWhenDeviceDoesntExist() { // @formatter:off given() .pathParam("apiVersion", apiVersion) .pathParam("uuid", NON_EXISTING_UUID) .body(new DevicePutJsonImpl()) .contentType(ContentType.JSON) .when() .put(URL) .then() .statusCode(HttpStatus.NOT_FOUND.value()) .body("message", equalTo(DEVICE_NOT_FOUND_EXCEPTION)); // @formatter:on }
@RequestMapping(value = "/pluginlogin", method = RequestMethod.POST) public ResponseEntity<Map<String, Object>> userPluginLogin( @RequestParam("email") String email, @RequestParam("password") String password) { Map<String, Object> map = new HashMap<String, Object>(); User user = embedDao.memoLogin(email, password); System.out.println(user); if (user != null) { map.put("MESSAGE", "MEMO HAS BEEN FOUND."); map.put("STATUS", "true"); map.put("DATA", user); return new ResponseEntity<Map<String, Object>>(map, HttpStatus.OK); } else { map.put("MESSAGE", "MEMO NOT FOUND."); map.put("STATUS", HttpStatus.NOT_FOUND.value()); return new ResponseEntity<Map<String, Object>>(map, HttpStatus.NOT_FOUND); } }
@RequestMapping( value = "/savememo", method = RequestMethod.POST, headers = "Accept=application/json") public ResponseEntity<Map<String, Object>> addMemo(@RequestBody Memo memo) { Map<String, Object> map = new HashMap<String, Object>(); if (memoDao.getMemoByUrl(memo.getDomain(), memo.getUrl(), memo.getUserid()) == null) { memoDao.insertMemo(memo); map.put("MESSAGE", "MEMO HAS BEEN CREATED."); map.put("STATUS", HttpStatus.CREATED.value()); return new ResponseEntity<Map<String, Object>>(map, HttpStatus.OK); } else { map.put("MESSAGE", "MEMO HAS NOT BEEN CREATED."); map.put("STATUS", HttpStatus.NOT_FOUND.value()); return new ResponseEntity<Map<String, Object>>(map, HttpStatus.NOT_ACCEPTABLE); } }
@RequestMapping( value = "/plugingetmemo", method = RequestMethod.POST, headers = "Accept=application/json") public ResponseEntity<Map<String, Object>> getMemo(@RequestBody Memo memo) { Map<String, Object> map = new HashMap<String, Object>(); List<Memo> memos = new ArrayList<Memo>(); memos = memoDao.pluginGetMemo(memo.getUserid(), memo.getUrl()); if (!memos.isEmpty()) { map.put("MESSAGE", "MEMO HAS BEEN FOUND."); map.put("STATUS", HttpStatus.CREATED.value()); map.put("DATA", memos); return new ResponseEntity<Map<String, Object>>(map, HttpStatus.OK); } else { map.put("MESSAGE", "MEMO NOT FOUND."); map.put("STATUS", HttpStatus.NOT_FOUND.value()); return new ResponseEntity<Map<String, Object>>(map, HttpStatus.NOT_FOUND); } }
/** * Testing controller geocode.json with valid data in case when requested object is not already * precashed in local DB. * * @throws ServletException * @throws IOException * @throws UnsupportedEncodingException * @throws JsonMappingException * @throws JsonGenerationException * @throws Exception */ @Test public void testGeoCodeXmlJsonValidDataNotCatchedCodeUnExistingLocation() throws JsonGenerationException, JsonMappingException, UnsupportedEncodingException, IOException, ServletException { // data preparing final String zipCode = "postalCode"; final String houseNumber = "44"; mockRequest.setMethod(RequestMethod.GET.name()); mockRequest.setRequestURI("/api/address.json"); mockRequest.setParameter("zipcode", zipCode); mockRequest.setParameter("number", houseNumber); // calling method final String responseContent = processRequest(null, null, mockRequest, mockResponse); // asserting Assert.assertEquals(HttpStatus.NOT_FOUND.value(), mockResponse.getStatus()); }
@ResponseBody @RequestMapping(value = "delete", method = RequestMethod.POST) public String delete(HttpServletRequest request, HttpServletResponse response) { JSONObject json = FormRequestUtil.parseData(request); Object _id = json.get("id"); if (_id == null) { response.setStatus(HttpStatus.BAD_REQUEST.value()); return "媒体Id不能为空"; } int id = Integer.parseInt(_id.toString()); Media media = service.getById(id); if (media != null) { service.delete(id); return media.toJson().toJSONString(); } else { response.setStatus(HttpStatus.NOT_FOUND.value()); return "媒体[id=" + _id + "]不存在"; } }
@ExceptionHandler(ContentNotFoundException.class) public ResponseEntity<ExceptionDTO> contentNotFound(ContentNotFoundException e) { ExceptionDTO exceptionDTO = new ExceptionDTO(e.getValue(), HttpStatus.NOT_FOUND.value()); return new ResponseEntity<>(exceptionDTO, HttpStatus.NOT_FOUND); }
@Override public Response toResponse(NotFoundException e) { return Response.status(HttpStatus.NOT_FOUND.value()) .entity(new ErrorWrapper(e.getMessage())) .build(); }
// -- Exception Handler @ResponseStatus(value = HttpStatus.NOT_FOUND) @ExceptionHandler(EmptyResultDataAccessException.class) public ErrorResponse notFoundError(final HttpServletRequest request) { return createErrorResponse(HttpStatus.NOT_FOUND.value(), "not_found", request); }