net.eusashead.hateoas.response.argumentresolver.EntityController.java Source code

Java tutorial

Introduction

Here is the source code for net.eusashead.hateoas.response.argumentresolver.EntityController.java

Source

package net.eusashead.hateoas.response.argumentresolver;

/*
 * #[license]
 * spring-responseentitybuilder
 * %%
 * Copyright (C) 2013 Eusa's Head
 * %%
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * %[license]
 */

import net.eusashead.hateoas.response.DeleteResponseBuilder;
import net.eusashead.hateoas.response.EntityResponseBuilder;
import net.eusashead.hateoas.response.OptionsResponseBuilder;
import net.eusashead.hateoas.response.PostResponseBuilder;
import net.eusashead.hateoas.response.PutResponseBuilder;
import net.eusashead.hateoas.test.model.Entity;

import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping(value = "/entity", consumes = "application/json;charset=utf-8")
public class EntityController {

    @RequestMapping(method = { RequestMethod.GET, RequestMethod.HEAD })
    public ResponseEntity<Entity> get(EntityResponseBuilder<Entity> builder) {
        return builder.entity(new Entity("foo")).etag().build();
    }

    @RequestMapping(method = RequestMethod.OPTIONS)
    public ResponseEntity<Entity> options(OptionsResponseBuilder<Entity> builder) {
        return builder.allow(HttpMethod.GET, HttpMethod.HEAD).entity(new Entity("foo")).build();
    }

    @RequestMapping(method = RequestMethod.POST)
    public ResponseEntity<Void> post(@RequestBody Entity entity, PostResponseBuilder builder) {
        return builder.location("/blah").build();
    }

    @RequestMapping(method = RequestMethod.PUT)
    public ResponseEntity<Void> put(@RequestBody Entity entity, PutResponseBuilder<Entity> builder) {
        return builder.build();
    }

    @RequestMapping(method = RequestMethod.DELETE)
    public ResponseEntity<Void> delete(DeleteResponseBuilder builder) {
        return builder.build();
    }
}