Example usage for org.springframework.http HttpHeaders setLocation

List of usage examples for org.springframework.http HttpHeaders setLocation

Introduction

In this page you can find the example usage for org.springframework.http HttpHeaders setLocation.

Prototype

public void setLocation(@Nullable URI location) 

Source Link

Document

Set the (new) location of a resource, as specified by the Location header.

Usage

From source file:fi.hsl.parkandride.front.HubController.java

@RequestMapping(method = POST, value = HUBS, produces = APPLICATION_JSON_VALUE)
public ResponseEntity<Hub> createHub(@RequestBody Hub hub, User currentUser, UriComponentsBuilder builder) {
    log.info("createHub");
    Hub newHub = hubService.createHub(hub, currentUser);
    log.info("createHub({})", newHub.id);

    HttpHeaders headers = new HttpHeaders();
    headers.setLocation(builder.path(HUB).buildAndExpand(newHub.id).toUri());
    return new ResponseEntity<>(newHub, headers, CREATED);
}

From source file:cn.cdwx.jpa.web.account.TaskRestController.java

@RequestMapping(method = RequestMethod.POST, consumes = MediaTypes.JSON)
public ResponseEntity<?> create(@RequestBody Task task, UriComponentsBuilder uriBuilder) {
    // JSR303 Bean Validator, RestExceptionHandler?.
    BeanValidators.validateWithException(validator, task);

    // ?//  w  w w  . j a va 2s  . c o  m
    taskService.saveTask(task);

    // Restful?url, ?id.
    String id = task.getId();
    URI uri = uriBuilder.path("/api/v1/task/" + id).build().toUri();
    HttpHeaders headers = new HttpHeaders();
    headers.setLocation(uri);

    return new ResponseEntity(headers, HttpStatus.CREATED);
}

From source file:com.gazbert.bxbot.rest.api.MarketConfigController.java

@RequestMapping(value = "/market/{marketId}", method = RequestMethod.PUT)
ResponseEntity<?> updateMarket(@AuthenticationPrincipal User user, @PathVariable String marketId,
        @RequestBody MarketConfig config) {

    final MarketConfig updatedMarketConfig = marketConfigService.updateMarket(config);
    final HttpHeaders httpHeaders = new HttpHeaders();
    httpHeaders.setLocation(ServletUriComponentsBuilder.fromCurrentRequest().path("/{marketId}")
            .buildAndExpand(updatedMarketConfig.getId()).toUri());
    return new ResponseEntity<>(updatedMarketConfig, httpHeaders, HttpStatus.NO_CONTENT);
}

From source file:$.TaskRestController.java

@RequestMapping(method = RequestMethod.POST, consumes = MediaTypes.JSON)
    public ResponseEntity<?> create(@RequestBody Task task, UriComponentsBuilder uriBuilder) {
        // JSR303 Bean Validator, RestExceptionHandler?.
        BeanValidators.validateWithException(validator, task);

        // ?/*from www .j  a  v a  2 s .c o  m*/
        taskService.saveTask(task);

        // Restful?url, ?id.
        Long id = task.getId();
        URI uri = uriBuilder.path("/api/v1/task/" + id).build().toUri();
        HttpHeaders headers = new HttpHeaders();
        headers.setLocation(uri);

        return new ResponseEntity(headers, HttpStatus.CREATED);
    }

From source file:com.jiwhiz.rest.user.PostCommentRestController.java

@RequestMapping(method = RequestMethod.POST, value = URL_USER_BLOGS_BLOG_COMMENTS, consumes = MediaType.APPLICATION_JSON_VALUE)
@Transactional//from w  w  w . j a v a  2 s  .co  m
public ResponseEntity<Void> postComment(@PathVariable("blogId") String blogId,
        @RequestBody CommentForm newComment) throws ResourceNotFoundException {
    UserAccount currentUser = getCurrentAuthenticatedUser();
    BlogPost blogPost = this.blogPostRepository.findOne(blogId);
    if (blogPost == null || !blogPost.isPublished()) {
        throw new ResourceNotFoundException("No published blog post with the id: " + blogId);
    }

    CommentPost commentPost = commentPostService.postComment(currentUser, blogPost, newComment.getContent());

    //send email to author if someone else posted a comment to blog.
    if (this.commentNotificationSender != null && !blogPost.getAuthor().equals(currentUser)) {
        this.commentNotificationSender.send(blogPost.getAuthor(), currentUser, commentPost, blogPost);
    }

    UserCommentResource resource = userCommentResourceAssembler.toResource(commentPost);
    HttpHeaders httpHeaders = new HttpHeaders();
    httpHeaders.setLocation(URI.create(resource.getLink(Link.REL_SELF).getHref()));

    return new ResponseEntity<>(httpHeaders, HttpStatus.CREATED);
}

From source file:com.gazbert.bxbot.rest.api.MarketConfigController.java

@RequestMapping(value = "/market/{marketId}", method = RequestMethod.POST)
ResponseEntity<?> createMarket(@AuthenticationPrincipal User user, @PathVariable String marketId,
        @RequestBody MarketConfig config) {

    final MarketConfig updatedMarketConfig = marketConfigService.createMarket(config);
    final HttpHeaders httpHeaders = new HttpHeaders();

    httpHeaders.setLocation(ServletUriComponentsBuilder.fromCurrentRequest().path("/{marketId}")
            .buildAndExpand(updatedMarketConfig.getId()).toUri());
    return new ResponseEntity<>(null, httpHeaders, HttpStatus.CREATED);
}

From source file:com.mycompany.springrest.controllers.UserController.java

@RequestMapping(value = "/user/", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Void> createUser(@RequestBody User user, UriComponentsBuilder ucBuilder) {
    logger.info("Creating User " + user.getUserName());
    if (userService.isUserExist(user)) {
        logger.info("A User with name " + user.getUserName() + " already exist");
        return new ResponseEntity<Void>(HttpStatus.CONFLICT);
    }//from  w  ww  . jav  a 2s  .c o m
    userService.addUser(user);

    HttpHeaders headers = new HttpHeaders();
    headers.setLocation(ucBuilder.path("/user/{id}").buildAndExpand(user.getId()).toUri());
    return new ResponseEntity<Void>(headers, HttpStatus.CREATED);
}

From source file:com.company.eleave.leave.rest.HolidayController.java

@RequestMapping(method = RequestMethod.POST)
public ResponseEntity<Long> createNew(@RequestBody HolidayDTO holiday) {
    final long holidayId = holidayService.createNew(mapper.toEntity(holiday));

    HttpHeaders headers = new HttpHeaders();
    headers.setLocation(UriComponentsBuilder.fromPath("/leaveTypes/{id}").buildAndExpand(holidayId).toUri());

    return new ResponseEntity<>(headers, HttpStatus.CREATED);
}

From source file:com.msyla.usergreeter.user.web.UserPreferenceResource.java

@RequestMapping(method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<UserPreferenceDto> create(
        @NotNull(message = "{userPreference.null}") @Valid @RequestBody UserPreferenceDto dto,
        UriComponentsBuilder builder) {//from   w w w  .  java  2 s. c  om
    UserPreferenceDto savedDto = service.create(dto);

    Link link = ControllerLinkBuilder.linkTo(UserPreferenceResource.class).slash(savedDto.getKey())
            .withSelfRel();
    savedDto.add(link);

    HttpHeaders headers = new HttpHeaders();
    headers.setLocation(builder.path("/api/user/preferences/{id}").buildAndExpand(savedDto.getKey()).toUri());
    return new ResponseEntity<>(savedDto, headers, HttpStatus.CREATED);
}

From source file:com.msyla.usergreeter.user.web.UserPreferenceResource.java

@RequestMapping(value = "/{id}", method = RequestMethod.PUT)
public ResponseEntity<UserPreferenceDto> update(@PathVariable Long id,
        @RequestBody @Valid UserPreferenceDto dto, UriComponentsBuilder builder) {

    UserPreferenceDto updatedDto = service.update(id, dto);

    Link link = ControllerLinkBuilder.linkTo(UserPreferenceResource.class).slash(updatedDto.getKey())
            .withSelfRel();//from ww w .  jav a  2  s  .  c  o m
    updatedDto.add(link);

    HttpHeaders headers = new HttpHeaders();
    headers.setLocation(builder.path("/api/user/preferences/{id}").buildAndExpand(updatedDto.getKey()).toUri());
    return new ResponseEntity<>(updatedDto, headers, HttpStatus.CREATED);
}