3

@Service and @Controller annotations are used for automatic bean detection using classpath scan in Spring framework.

So I tried below four use cases but I am bit confused with case 4 as it gives me 404 error.

use case 1: @Controller & class level @RequestMapping

@Controller
@RequestMapping(value = "/home")
public class MyController
{
...
}

Result:

http://localhost:8080/MyApp/home/helloWorld/va ---> Hello va

use case 2: @Service & class level @RequestMapping

@Service
@RequestMapping(value = "/home")
public class MyController
{
...
}

Result:

http://localhost:8080/MyApp/home/helloWorld/va ---> Hello va

use case 3: @Controller & no class level @RequestMapping

@Controller
public class MyController
{
...
}

Result:

http://localhost:8080/MyApp/helloWorld/va ---> Hello va

use case 4: @Service & no class level @RequestMapping

@Service
public class MyController
{
...
}

Result:

http://localhost:8080/MyApp/helloWorld/va ---> error 404

code:

@Service
@RequestMapping(value = "/home")
public class MyController
{

    @RequestMapping(value = "/helloWorld/{Name}", method = RequestMethod.GET)
    public @ResponseBody String HelloWorld(@PathVariable("Name") String name)
    {
        return "Hello "+name;
    }
}

So in short when using @Service, if I dont use @RequestMapping at class level am getting 404 error.

Vaibs
  • 1,546
  • 9
  • 31

1 Answers1

1

A bean is considered a request handler if it has either @Controller or @RequestMapping at class level.

a better oliver
  • 26,330
  • 2
  • 58
  • 66
  • either? AFAIK we need the Controller so after enabling automatic scanning, spring will import the bean in container.By using RequestMapping we can map the incoming request for the app.Also my question is related to 404 error after using @service without class level requestMapping. – Vaibs Sep 13 '16 at 07:40
  • @Vaibs I wrote bean. That implies the class is already considered by spring. You don't need `@Controller` to declare a bean, as you have demonstrated yourself in your question. _"Also my question is related to 404 error after using @service without class level requestMapping"_ And I answered that question. – a better oliver Sep 13 '16 at 07:49
  • you are doing configuration through xml or java class? – Vaibs Sep 13 '16 at 08:21
  • @Vaibs In example 4 you annotated `MyController` with `@Service`, so it's a bean. You didn't annotate it with `@Controller` or `@RequestMapping`, so that bean won't handle any requests. Therefore you get a 404, your request is not being handled. If you use XML or Java config is irrelevant. – a better oliver Sep 13 '16 at 08:36
  • Thank you for explanation . In short "@service" annotation works only when we use "@RequestMapping" at class level but I did not find any documentation or reference which shows this difference ,also as I know difference between "@service" and "@controller" is for classification only. – Vaibs Sep 13 '16 at 09:20
  • @Vaibs _"as I know difference between "@service" and "@controller" is for classification only."_ For Spring Core, yes. For Spring MVC, no. I was surprised that your example 2 works at all. – a better oliver Sep 13 '16 at 10:21