Example usage for org.springframework.web.servlet.mvc.method.annotation MvcUriComponentsBuilder fromMethodName

List of usage examples for org.springframework.web.servlet.mvc.method.annotation MvcUriComponentsBuilder fromMethodName

Introduction

In this page you can find the example usage for org.springframework.web.servlet.mvc.method.annotation MvcUriComponentsBuilder fromMethodName.

Prototype

public static UriComponentsBuilder fromMethodName(UriComponentsBuilder builder, Class<?> controllerType,
        String methodName, Object... args) 

Source Link

Document

An alternative to #fromMethodName(Class,String,Object) that accepts a UriComponentsBuilder representing the base URL.

Usage

From source file:ch.heigvd.gamification.api.BadgesEndpoint.java

@Override
@RequestMapping(method = RequestMethod.POST)
public ResponseEntity<Void> badgesPost(
        @ApiParam(value = "Badge to add", required = true) @RequestBody BadgeDTO body,
        @ApiParam(value = "token that identifies the app sending the request", required = true) @RequestHeader(value = "X-Gamification-Token", required = true) String xGamificationToken) {
    AuthenKey apiKey = authenKeyRepository.findByAppKey(xGamificationToken);

    if (apiKey == null) {
        return new ResponseEntity(HttpStatus.UNAUTHORIZED);
    }//w w  w . java 2  s  .  c om

    Application app = apiKey.getApp();

    if (body != null && app != null) {

        if (badgeRepository.findByNameAndApp(body.getName(), app) != null) {
            return new ResponseEntity("name already use", HttpStatus.UNPROCESSABLE_ENTITY);
        }
        Badge badge = new Badge();
        badge.setDescription(body.getDescription());
        badge.setName(body.getName());
        badge.setImage(body.getImageURI());
        badge.setApp(app);
        badgeRepository.save(badge);

        HttpHeaders responseHeaders = new HttpHeaders();

        UriComponents uriComponents = MvcUriComponentsBuilder
                .fromMethodName(BadgesEndpoint.class, "badgesBadgeIdGet", 1, badge.getId()).build();

        URI locationUri = uriComponents.toUri();
        responseHeaders.add("Location", uriComponents.toString());
        return new ResponseEntity<>(responseHeaders, HttpStatus.CREATED);

    } else {
        return new ResponseEntity("no content is available", HttpStatus.BAD_REQUEST);
    }
}