abujafar
abujafar

Reputation: 131

Spring boot annotation confusion

I am creating a spring boot application based on an assignment in an online MOOC. The course is over and I am just trying the assignment on my own.

THis is my pom.xml file:

    <modelVersion>4.0.0</modelVersion>
  <groupId>org.springframework.samples.service.service</groupId>
  <artifactId>VideoManagerServer</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>

    <properties>

        <!-- Generic properties -->
        <java.version>1.6</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

        <!-- Web -->
        <jsp.version>2.2</jsp.version>
        <jstl.version>1.2</jstl.version>
        <servlet.version>2.5</servlet.version>


        <!-- Spring -->
        <spring-framework.version>3.2.3.RELEASE</spring-framework.version>

        <!-- Hibernate / JPA -->
        <hibernate.version>4.2.1.Final</hibernate.version>

        <!-- Logging -->
        <logback.version>1.0.13</logback.version>
        <slf4j.version>1.7.5</slf4j.version>

        <!-- Test -->
        <junit.version>4.11</junit.version>

    </properties>

    <dependencies>

        <!-- Spring MVC -->

        <!-- Other Web dependencies -->

        <!-- Spring and Transactions -->

        <!-- Logging with SLF4J & LogBack -->

        <!-- Hibernate -->


        <!-- Test Artifacts -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>${spring-framework.version}</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>1.2.5.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>4.1.7.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>4.1.7.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jersey</artifactId>
            <version>1.2.5.RELEASE</version>
        </dependency>
    </dependencies> 

THis is my controller:

import java.util.ArrayList;


import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.hakeem.videoserver.model.Video;
import com.hakeem.videoserver.service.VideoService;


@RestController
@RequestMapping(value="/api",consumes=MediaType.APPLICATION_JSON_VALUE,produces=MediaType.APPLICATION_JSON_VALUE)
public class VideoController {

    @Autowired
    VideoService videoService;

    @RequestMapping(value="/add",method=RequestMethod.POST)
    public void addVideo(@RequestBody Video video){

        videoService.addVideo(video);

    }

    @RequestMapping(value="/all",method=RequestMethod.GET)
    public @ResponseBody ArrayList<Video> getAllVideos(HttpServletResponse response){
        ArrayList<Video> videos;
        videos = videoService.getAllVideos();
        if(videos.size() == 0){
            response.setStatus(HttpStatus.NO_CONTENT.value());
        }
        return videos;
    }

    @RequestMapping(value="/delete",method=RequestMethod.DELETE)
    public void deleteVideo(@RequestBody Video video){

        videoService.deleteVideo(video);

    }

    @RequestMapping(value="/find/{id}")
    public @ResponseBody Video findVideo(@PathVariable int id, HttpServletResponse response){

        return videoService.findVideo(id);

    }

    @RequestMapping(value="/testing",method=RequestMethod.GET,produces=MediaType.APPLICATION_JSON_VALUE)
    public @ResponseBody int testEndPoint(HttpServletResponse response){

        return 10;
    }


}

The problem is my addVideo endpoint only works when I annotate with @RestController but not when I annotate with @Controller.

However my testEndpoint method works when I annotate with @RestController and @Controller

However if I add @ResponseBody to the Class or the addVideo method then it works.

Using the Postmat plugin on Chrome, when I send the following Post Request: http://localhost:8080/api/add With this body for a video: { "id":1, "title":"test", "contentType":"test1", "dataUrl":"testurl", "starRating":"5", "duration":7

} I get this message in Postman:

{ "timestamp": 1440349612613,
"status": 405,
"error": "Method Not Allowed",
"exception":"org.springframework.web.HttpRequestMethodNotSupportedException", "message": "Request method 'POST' not supported", "path": "/api/add" }

And this message in my eclipse console: WARN PageNotFound - Request method 'POST' not supported

But the video is add and can be retrieved without any errors if I had @ResponseBody to the class or the method return type. Therefore the main question is: Why do I need to add @ResponseBody if the addVideo is not returning anything.

Upvotes: 2

Views: 1488

Answers (1)

Gergely Bacso
Gergely Bacso

Reputation: 14651

There were two questions asked here:

  1. Why @RestController works, but @Controller does not?

If you check the documentation, you will see that RestController is

A convenience annotation that is itself annotated with @Controller and @ResponseBody.

  1. Why do you need to add @ResponseBody if the return type is void?

The reason for this is that even if there is no actual value you want to return, there will be a response sent to the client (at least an HTTP OK). For this reason you need to use one of these:

  • @ResponseBody
  • @ResponseStatus(value = HttpStatus.OK)

Upvotes: 1

Related Questions