1
votes

I've created a Spring MVC web app for my university project with Maven using tomcat. However, I've been struggling with getting constant 404 errors when I'm accessing the /user/info URL and also all other URLs. The /user/info URL should be mapped to the ProfileService class and the getUserInfo() method, but unfortunately it fails and I'm ending up with a 404 error.

I'm trying to figure out what is issue that is causing 404 errors. I have tried this url: localhost:8080/user/info Changing web.xml url-mapping to "/*" did not helped.

Is the problem located in bad configuration of the web.xml or spring-context.xml files that prevent the URL to be mapped?

When I'm accessing the / URL, then the index.jsp file is mapped and it's is working fine.

Thanks in advance.

spring-context.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans
        xmlns="http://www.springframework.org/schema/mvc"
        xmlns:beans="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <annotation-driven />
    <context:component-scan base-package="ie" />

</beans:beans>

web.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">

    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>WEB-INF/spring-context.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>

ProfileService.java file:

package ie.services;

import ie.logic.Loghme;
import ie.services.responses.CreditInfo;
import ie.services.responses.StatusCode;
import ie.services.responses.UserInfo;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

@RestController
public class ProfileService {

    @RequestMapping(value = "/user/info", method = RequestMethod.GET,
            produces = MediaType.APPLICATION_JSON_VALUE)
    public UserInfo getUserInfo() {
        return new UserInfo(Loghme.getInstance().getLoginnedUser());
    }

    @RequestMapping(value = "/user/credit", method = RequestMethod.GET,
            produces = MediaType.APPLICATION_JSON_VALUE)
    public CreditInfo getUserCredit(){
        return new CreditInfo(Loghme.getInstance().getLoginnedUser().getCredit());
    }
}

1

1 Answers

0
votes

Your mapping is not quite right. You have a @PathVariable as method parameter that you didn't specify in your path. So it should more read like this (look at the value= from @RequestMapping).

 @RequestMapping(value = "/user/info/{s}", method = RequestMethod.GET,
            produces = MediaType.APPLICATION_JSON_VALUE)
    public UserInfo getUserInfo(@PathVariable String s) {
        return new UserInfo(Loghme.getInstance().getLoginnedUser());
    }

Which then, however, means that you need to put it there when making a request, like GET "/user/info/hello".

If you are not using it, however, you can simply drop it from the method and from the string and then can call "/user/info".