/** * Prepare a builder from the host, port, scheme, context path, and * servlet mapping of the given HttpServletRequest. * <p>If the servlet is mapped by name, e.g. {@code "/main/*"}, the path * will end with "/main". If the servlet is mapped otherwise, e.g. * {@code "/"} or {@code "*.do"}, the result will be the same as * if calling {@link #fromContextPath(HttpServletRequest)}. */ public static ServletUriComponentsBuilder fromServletMapping(HttpServletRequest request) { ServletUriComponentsBuilder builder = fromContextPath(request); if (StringUtils.hasText(new UrlPathHelper().getPathWithinServletMapping(request))) { builder.path(request.getServletPath()); } return builder; }
/** * Prepare a builder from the host, port, scheme, context path, and * servlet mapping of the given HttpServletRequest. * <p>If the servlet is mapped by name, e.g. {@code "/main/*"}, the path * will end with "/main". If the servlet is mapped otherwise, e.g. * {@code "/"} or {@code "*.do"}, the result will be the same as * if calling {@link #fromContextPath(HttpServletRequest)}. */ public static ServletUriComponentsBuilder fromServletMapping(HttpServletRequest request) { ServletUriComponentsBuilder builder = fromContextPath(request); if (StringUtils.hasText(new UrlPathHelper().getPathWithinServletMapping(request))) { builder.path(request.getServletPath()); } return builder; }
public static UriComponentsBuilder getURIBuilder(String path, boolean zoneSwitchPossible) { UriComponentsBuilder builder = ServletUriComponentsBuilder.fromCurrentContextPath().path(path); if (zoneSwitchPossible) { String host = builder.build().getHost(); IdentityZone current = IdentityZoneHolder.get(); if (host != null && !IdentityZoneHolder.isUaa()) { if (!host.startsWith(current.getSubdomain() + ".")) { host = current.getSubdomain() + "." + host; builder.host(host); } } } return builder; }
@Test // SPR-10272 public void pathExtension() { this.request.setRequestURI("/rest/books/6.json"); ServletUriComponentsBuilder builder = ServletUriComponentsBuilder.fromRequestUri(this.request); String extension = builder.removePathExtension(); String result = builder.path("/pages/1.{ext}").buildAndExpand(extension).toUriString(); assertEquals("http://localhost/rest/books/6/pages/1.json", result); }
/** * Returns a new {@link UriComponentsBuilder} for the base URI. If the base URI is not absolute, it'll lokup the URI * for the current servlet mapping and extend it accordingly. * * @return */ public UriComponentsBuilder getUriComponentsBuilder() { if (baseUri.isAbsolute()) { return UriComponentsBuilder.fromUri(baseUri); } return ServletUriComponentsBuilder.fromCurrentServletMapping().path(baseUri.toString()); } }
/** * Returns the View to redirect to to access the HAL browser. * * @param request must not be {@literal null}. * @param browserRelative * @return */ private View getRedirectView(HttpServletRequest request, boolean browserRelative) { ServletUriComponentsBuilder builder = ServletUriComponentsBuilder.fromRequest(request); UriComponents components = builder.build(); String path = components.getPath() == null ? "" : components.getPath(); if (!browserRelative) { builder.path(BROWSER); } builder.path(INDEX); builder.fragment(browserRelative ? path.substring(0, path.lastIndexOf("/browser")) : path); return new RedirectView(builder.build().toUriString()); } }
private String buildSignupUrl(HttpServletRequest request) { if (signupUrl.startsWith("http://") || signupUrl.startsWith("https://")) { return signupUrl; } if (!signupUrl.startsWith("/")) { return ServletUriComponentsBuilder.fromContextPath(request).path("/" + signupUrl).build().toUriString(); } return ServletUriComponentsBuilder.fromContextPath(request).path(signupUrl).build().toUriString(); }
@PostMapping public ResponseEntity<?> postBook(@RequestBody Book book) { bookService.saveBook(book); URI location = ServletUriComponentsBuilder .fromCurrentRequest() .path("/{id}") .buildAndExpand(book.getId()) .toUri(); return ResponseEntity .created(location) .body(book); }
@RequestMapping(method = RequestMethod.POST, value = "/{user}/customers") ResponseEntity<Customer> addCustomer(@PathVariable Long user, @RequestBody Customer c) { Customer customer = crmService.addCustomer(user, c.getFirstName(), c.getLastName()); URI uriOfNewResource = ServletUriComponentsBuilder.fromCurrentContextPath() .path("/users/{user}/customers/{customer}") .buildAndExpand(user, customer.getId()) .toUri(); HttpHeaders httpHeaders = new HttpHeaders(); httpHeaders.setLocation(uriOfNewResource); return new ResponseEntity<>(customer, httpHeaders, HttpStatus.CREATED); }
@PostMapping("/students") public ResponseEntity<Object> createStudent(@Valid @RequestBody Student student) { Student savedStudent = studentRepository.save(student); URI location = ServletUriComponentsBuilder.fromCurrentRequest().path("/{id}") .buildAndExpand(savedStudent.getId()).toUri(); return ResponseEntity.created(location).build(); }
@PostMapping("/users") public ResponseEntity<Object> createUser(@Valid @RequestBody User user) { User savedUser = service.save(user); // CREATED // /user/{id} savedUser.getId() URI location = ServletUriComponentsBuilder .fromCurrentRequest() .path("/{id}") .buildAndExpand(savedUser.getId()).toUri(); return ResponseEntity.created(location).build(); } }
@PostMapping("/jpa/users") public ResponseEntity<Object> createUser(@Valid @RequestBody User user) { User savedUser = userRepository.save(user); URI location = ServletUriComponentsBuilder.fromCurrentRequest().path("/{id}").buildAndExpand(savedUser.getId()) .toUri(); return ResponseEntity.created(location).build(); }
@RequestMapping(path = "/workbooks/{workbookId}/comments", method = RequestMethod.POST) public @ResponseBody ResponseEntity<?> createComments(@PathVariable("workbookId") String workbookId, @RequestBody Comment comment) { comment.setDomainType(WORKBOOK); comment.setDomainId(workbookId); commentRepository.saveAndFlush(comment); final URI location = ServletUriComponentsBuilder .fromCurrentServletMapping().path("/api/workbooks/{workbookId}/comments/{commentId}").build() .expand(workbookId, comment.getId()).toUri(); return ResponseEntity.created(location).build(); }
@PostMapping("/students") public ResponseEntity<Object> createStudent(@RequestBody Student student) { Student savedStudent = studentRepository.save(student); URI location = ServletUriComponentsBuilder.fromCurrentRequest().path("/{id}") .buildAndExpand(savedStudent.getId()).toUri(); return ResponseEntity.created(location).build(); }
@PostMapping("/students") public ResponseEntity<Object> createStudent(@RequestBody Student student) { Student savedStudent = studentRepository.save(student); URI location = ServletUriComponentsBuilder.fromCurrentRequest().path("/{id}") .buildAndExpand(savedStudent.getId()).toUri(); return ResponseEntity.created(location).build(); }
@PostMapping("/students") public ResponseEntity<Object> createStudent(@RequestBody Student student) { Student savedStudent = studentRepository.save(student); URI location = ServletUriComponentsBuilder.fromCurrentRequest().path("/{id}") .buildAndExpand(savedStudent.getId()).toUri(); return ResponseEntity.created(location).build(); }
@PostMapping("/students") public ResponseEntity<Object> createStudent(@RequestBody Student student) { Student savedStudent = studentRepository.save(student); URI location = ServletUriComponentsBuilder.fromCurrentRequest().path("/{id}") .buildAndExpand(savedStudent.getId()).toUri(); return ResponseEntity.created(location).build(); }
@PostMapping("/students") public ResponseEntity<Object> createStudent(@RequestBody Student student) { Student savedStudent = studentRepository.save(student); URI location = ServletUriComponentsBuilder.fromCurrentRequest().path("/{id}") .buildAndExpand(savedStudent.getId()).toUri(); return ResponseEntity.created(location).build(); }
@PostMapping("/students/{studentId}/courses") public ResponseEntity<Void> registerStudentForCourse( @PathVariable String studentId, @RequestBody Course newCourse) { Course course = studentService.addCourse(studentId, newCourse); if (course == null) return ResponseEntity.noContent().build(); URI location = ServletUriComponentsBuilder.fromCurrentRequest().path( "/{id}").buildAndExpand(course.getId()).toUri(); return ResponseEntity.created(location).build(); }
@PostMapping("/students/{studentId}/courses") public ResponseEntity<Void> registerStudentForCourse( @PathVariable String studentId, @RequestBody Course newCourse) { Course course = studentService.addCourse(studentId, newCourse); if (course == null) return ResponseEntity.noContent().build(); URI location = ServletUriComponentsBuilder.fromCurrentRequest().path( "/{id}").buildAndExpand(course.getId()).toUri(); return ResponseEntity.created(location).build(); }