1

I'm using spring-boot and want to integrate a simple REST service as follows.

    @Controller
    @RequestMapping("/content")
    public class MyServiceRest extends SpringBeanAutowiringSupport {
        @RequestMapping(method = RequestMethod.GET)
        public String test() {
            return "OK";
        }
    }

Result: both localhost:8080/<app-name>/services/content results "No service was found.". Why?

Do I have to explicit publish the service somehow?

Maybe it is due to my dispatcher servlet?

@Bean
public ServletRegistrationBean dispatcherServletRegistration() {
    ServletRegistrationBean registration = new ServletRegistrationBean(new CXFServlet(), "/services/*");
    registration.setName(DispatcherServletAutoConfiguration.DEFAULT_DISPATCHER_SERVLET_REGISTRATION_BEAN_NAME);
    return registration;
}
membersound
  • 81,582
  • 193
  • 585
  • 1,120
  • 1
    Generally the convention is localhost:8080/ApplicationName/content, give that a try? – ConMan Jan 19 '15 at 10:53
  • Same result unfortunatelly... – membersound Jan 19 '15 at 10:55
  • has your error changed from 404 to "No service was found' msg? – Pranalee Jan 19 '15 at 11:18
  • Yes I noticed I configured the dispatcher servlet, and thus changing the path to `.../services/content` will give me the correct path. Anyhow the service is not found... – membersound Jan 19 '15 at 11:19
  • 2
    `@Controller` is a generic Spring MVC annotation, you need to annotate your config class with `@EnableWebMvc` regardless of using Spring Boot or Tomcat if you want Spring to load those. If you want Spring Boot to autoconfigure your REST controllers then change your `@Controller` annotation by `@RestController` and use `@EnableAutoConfiguration` in your config class. – Alonso Dominguez Jan 19 '15 at 11:24
  • Of course I'm using `@EnableAutoConfiguration` with spring-boot. Tried changing the rest service to `@RestController`, but still same result. Maybe I have to explicit register the REST service to my custom dispatcher servlet somehow?? – membersound Jan 19 '15 at 11:26
  • I just saw your last edit. I don't understand why you are trying to load the CXFServlet as that is a different technology. Spring is able to serve REST without it and, in my opinion, you should remove it. Additionally, Spring needs more information about what is your service trying to consume and what is trying to produce (XML, JSON, Plain Text ??). So you must add that information to the `@RequestMapping`. Moreover, what is that "OK" you're returning? Is it a view name or the response body? If the latter then add `@ResponseBody` to the method, otherwise Spring will believe is the former. – Alonso Dominguez Jan 19 '15 at 11:29
  • ah, and I think you should add a bigger chunk of code from your config class, Spring has too many ways of being configured and it's quite difficult to help without knowing what way are you doing it. – Alonso Dominguez Jan 19 '15 at 11:32
  • I'm using the `CXFServlet` to provide a `soap` service within the same application context and webserver. So I cannot remove that. I tried adding the `@ResponseBody` but that again did not change anything. How could I integrate the default spring REST servlet when using the CXFServlet side by side? I found out that when removing the dispatcher servet, the REST service would work as expected. – membersound Jan 19 '15 at 11:36
  • you have a conflict between that servlet and the standard Spring Dispatcher Servlet. Spring is finding out that you are already configuring a servlet for the same context and so it's not attempting to configure the one you want for your REST services. You need a clear separation between the different areas in your application and if you want to live them together you will need two top level Spring contexts and likely moving the common low-level components to a different config class that could be imported from the two top level ones. – Alonso Dominguez Jan 19 '15 at 11:39
  • Could I somehow create the `CXFServlet` for use with soap with a different path, and aside use the default `REST` servlet with default path? – membersound Jan 19 '15 at 11:42
  • both of your servlets will need a specific path, you can't have two servlets in the same application in which one of the uses the `/` path as the servlet container is not able to determine if a request with path `/foo` should go to the default servlet or to the other one. This decisions is taken by the servlet container, not Spring, and the container has no knowledge whatsoever of your Spring request mappings. So, in essence, you can not do it. Now someone can come and say that you could use a hack with a filter and imo you should avoid that. – Alonso Dominguez Jan 19 '15 at 11:47
  • Well but I could map each servlet to its specific path without having a servlet mapping to the root path `/`? That would be fine also. I yes, how could I do this? – membersound Jan 19 '15 at 12:02
  • Given that the problem seems to be the attempt to override the dispatcher servlet configured by Spring Boot, it's probably worth pointing out that Spring Boot provides a starter for SOAP services too: http://spring.io/guides/gs/producing-web-service/ – Steve Jan 19 '15 at 12:15
  • Spring only supports wsd-first approach for soap webservices. I need java-first and thus have to use the CXFServlet. – membersound Jan 19 '15 at 12:18
  • You can follow the steps in this..http://72.251.248.20:81/wordpress/index.php/2015/10/09/stand-alone-restfull-web-service-spring-boot/ – Ruchira Gayan Ranaweera Oct 12 '15 at 04:12

6 Answers6

1

Since you are using Spring Boot, make sure that your application is correctly setup by adding the correct annotations. For instance,

@EnableAutoConfiguration
@EnableWebMvc
@Configuration
@ComponentScan
/*
 * Application Setups using Spring boot.
 */
public class Application{

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

@EnableWebMvc is the annotation to add for using Spring MVC with Spring boot. And then you can define your controller as you did in your question.

Dimitri
  • 8,122
  • 19
  • 71
  • 128
  • Is `@EnableWebMvc` a requirement for REST? I don't have this annotation so far. – membersound Jan 19 '15 at 11:03
  • I ment: I did not set up that annotation so far. Of course I have it on the classpath by spring-boot. – membersound Jan 19 '15 at 11:04
  • Yes, this annotation is necessary if you want to setup MVC with Spring boot. Take a look at this documentation http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/servlet/config/annotation/EnableWebMvc.html – Dimitri Jan 19 '15 at 11:05
  • What if my application config already `extends SpringBootServletInitializer`, which is the case? Then I could not also extend `RepositoryRestMvcConfiguration`... – membersound Jan 19 '15 at 11:06
0

you should also add url mapping for your method

@RequestMapping(method = RequestMethod.GET, value = "url_here", try

@RequestMapping(method = RequestMethod.POST, value = "/",
Pranalee
  • 3,389
  • 3
  • 22
  • 36
0

add package with controller class to @Component scan in main class like: @ComponentScan( basePackages = { "your.package.with.controller" } ), this happens when spring didn't initialize (doesn't know about) controller

Cuzz
  • 428
  • 2
  • 4
  • 20
  • try to run `ApplicationContext applicationContext = SpringApplication.run( Application.class, args ); MyServiceRest controller = applicationContext.getBean( MyServiceRest.class );` , if it can't find controller something wrong with component scan – Cuzz Jan 19 '15 at 11:09
0

In the latest version of Spring Boot, that I am currently using, the web Service would address be http://localhost:8080/content

Also, the class I use to launch the service looks as follows:

@ComponentScan("eu.buzea")
@EnableAutoConfiguration
@EnableTransactionManagement
@SpringBootApplication
public class Application{

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}
0

enter image description here

Source Code

https://drive.google.com/open?id=0BzBKpZ4nzNzUWmJmOTFwbTFjWWM

using Swagger

http://localhost:7070/swagger-ui.html#/

**Cheers*

Keshav Gera
  • 10,807
  • 1
  • 75
  • 53
0

As of current spring-boot.1.5.6 there is no requirement using cxf.

Just use a @RestController with @GetMapping, and be happy to access localhost:8080/content.

membersound
  • 81,582
  • 193
  • 585
  • 1,120