0

I am trying to expose a REST API via Spring MVC+REST with some Spring Security configuration. However, I am getting 404.

Here's the code.

web.xml

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app ...>
    <display-name>REST Project</display-name>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring/applicationContext.xml</param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <servlet>
        <servlet-name>rest</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>rest</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>

rest-servlet:

<?xml version="1.0" encoding="UTF-8"?>
<beans ... />
    <mvc:annotation-driven />
  </beans>

mvc-config:

<?xml version="1.0" encoding="UTF-8"?>

<beans ...>

    <mvc:annotation-driven />
    <bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!-- Example: a logical view name of 'showMessage' is mapped to '/WEB-INF/jsp/showMessage.jsp' -->
        <property name="prefix" value="/WEB-INF/view/" />
        <property name="suffix" value=".jsp" />
    </bean>
</beans>

applicationContext:

<?xml version="1.0" encoding="UTF-8"?>
<beans ... ">
    <context:component-scan base-package="org.my.project" />
</beans>

Finally, Service REST controller

@RestController
@RequestMapping("/service")
public class Service {

    @RequestMapping(value = "/public/{name}", method = RequestMethod.GET)
    public String storeEntityPublic(@PathVariable String name) {
        // do something

        return result;
    }
}

URL: localhost:8080//service/public/aname

What am I missing?

Aniket Kulkarni
  • 12,825
  • 9
  • 67
  • 90
Manu
  • 4,019
  • 8
  • 50
  • 94
  • These xml configurations are just something I would want to see less and less nowdays. Why wouldn't you use Java based configurations? – Vaelyr Jan 31 '15 at 13:05
  • Because I believe configuration should be done with metadata, i.e. annotations or XML. It's a matter of preference. If there is anything I can move from XML to annotations without introducing Java configuration classes, please let me know. – Manu Jan 31 '15 at 13:09
  • Have a look at the java based config first. It generally doesn't require additional config classes, just annotations. – Dave Jan 31 '15 at 13:19
  • Ok, I will try, but I necessarily need to fix the XML first – Manu Jan 31 '15 at 13:30
  • Your components are detected in the root context (i.e. the `ContextLoaderListener`) those aren't automatically detected by the `RequestMappingHandlerMapping` bean (registered through ``.). Scan for `@Controller`s in your servlet based config, and everything but `@Controller` in your root config. – M. Deinum Jan 31 '15 at 13:30
  • Another thing your `mvc-config.xml` seems pretty useless as nothing is loading it. – M. Deinum Jan 31 '15 at 13:31
  • You can move everything in there to Java based config. If you don't want much configuration at all, choose Spring Boot which does it all automatically for you, and if you need anything to be configured manually you can still do it. – Vaelyr Jan 31 '15 at 13:44

1 Answers1

-1

In your localhost:8080/service/public/aname, context name is missing, that's why you are getting the 404 error. The URL should be like localhost:8080/abc/service/public/aname, here abc is my project or context name. So please add context name and hit new url again.

ghdalum
  • 891
  • 5
  • 17
Rammy
  • 1