Kroxitrock
Kroxitrock

Reputation: 35

Spring can't get mapping

I am trying to build a REST API with Spring and Hibernate.

When I run the server it returns no errors, but I can't access my links.

Here is my config file:

spring.xml

<mvc:annotation-driven />
<context:component-scan base-package="org.bloggy.spring" />
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="hibernateProperties">
        <props>
            <prop key="hbm2ddl.auto">create-drop</prop>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
            <prop key="show_sql">true</prop>
        </props>
    </property>
</bean>

<bean id="dataSource"
    class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName"  value="com.mysql.jdbc.Driver"/>
    <property name="url" value="jdbc:mysql://localhost:3306/my_bloggy?createDatabaseIfNotExist=true" />
    <property name="username" value="root" />
    <property name="password" value="root" />
 </bean>

<bean id="txManager"
      class="org.springframework.transaction.jta.JtaTransactionManager" />

And that is my web.xml:

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<!-- Processes application requests -->

<servlet>
    <servlet-name>appServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>WEB-INF/resources/spring.xml</param-value>
        <param-value>WEB-INF/resources/dao.xml</param-value>
        <param-value>WEB-INF/resources/service.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>appServlet</servlet-name>
    <url-pattern>/api/*</url-pattern>
</servlet-mapping>

Controller:

@Controller
@RequestMapping(value = "users")
public class UserController {
  @Autowired
  private UserService userService;

  @RequestMapping(value = "/", method = RequestMethod.GET)
  public List<User> getUsers() {
    return userService.listUsers();
  }
}

Upvotes: 0

Views: 2290

Answers (3)

Kid101
Kid101

Reputation: 1470

As visible in logs the application MyBloggy doesn't actually initialize any spring beans. There is a problem with cache or the war file deployed on the tomcat. Ideally, At the time of deploying a war all the beans initialized can be seen in the logs unless disabled. Make sure that you are deploying a correct war on the server.

Upvotes: 1

Deepak Singh
Deepak Singh

Reputation: 411

As @Ken mentioned In order to expose your API as a REST AP you need to use @RestController or if you want to use @Controller then use @ResponseBody annotation.

With regards to your issue I think you need to define your RequestMapping as

@Controller
@RequestMapping(value = "api")
public class UserController {
  @Autowired
  private UserService userService;

  @RequestMapping(value = "/users", method = RequestMethod.GET)
  public List<User> getUsers() {
    return userService.listUsers();
  }
}

Or

@Controller
@RequestMapping(value = "api/users")
public class UserController {
  @Autowired
  private UserService userService;

  @RequestMapping(method = RequestMethod.GET)
  public List<User> getUsers() {
    return userService.listUsers();
  }
}

Please see if it works.

Upvotes: 1

khoibv
khoibv

Reputation: 369

You are using @Controller annotation, but if you want expose REST webservice, you need use @RestController or @ResponseBody

@RestController
@RequestMapping(value = "users")
public class UserController

Upvotes: 0

Related Questions