vendredi 6 mars 2015

RESTful web-service not found when I deploy app in eclipse

So, I have a Spring Maven web-app with a handful of RESTful web-services. For testing, this project is in Spring (4.1.4.RELEASE). I am using the latest STS (Spring-Eclipse) tool, and I am using Tomcat 8 for the server.


My UserController is designed as follows:



@Controller
@RequestMapping("/users")
public class UserController {
private final static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

@Autowired
private IUserService service;

@RequestMapping(value = "", method = RequestMethod.GET, headers = "Accept=application/json")
public @ResponseBody
ArrayList<UserEntity> getUserList()
{
System.out.println("UserController: getUserList: START");
ArrayList<UserEntity> userEntityList = (ArrayList) service.getAllUsers();
return userEntityList;
}

@RequestMapping(value = "/", method = RequestMethod.GET, headers = "Accept=application/json")
public @ResponseBody
ArrayList<UserEntity> getAllUsers()
{
System.out.println("UserController: getAllUsers: START");
ArrayList<UserEntity> userEntityList = (ArrayList) service.getAllUsers();
return userEntityList;
}


I have a test which runs when I build the app with maven, and this works great:



@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(locations =


{ "classpath:/spring/angular-context.xml", "file:src/main/webapp/WEB-INF/springmvc-servlet.xml" }) @Transactional public class BaseControllerTests extends TestCase {



@Test
public void testMockGetUserList1() throws Exception
{
MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders.get("/users/");
this.mockMvc.perform(requestBuilder).andDo(print()).andExpect(status().isOk());
}

@Test
public void testMockGetUserList2() throws Exception
{
MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders.get("/users");
this.mockMvc.perform(requestBuilder).andDo(print()).andExpect(status().isOk());
}
}


The web-xml file looks like:



<web-app>

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:/spring/angular-context.xml</param-value>
</context-param>
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>classpath:/logging/log4j-config.xml</param-value>
</context-param>

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

<!-- Servlets -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet>
<servlet-name>jUnitHostImpl</servlet-name>
<servlet-class>com.google.gwt.junit.server.JUnitHostImpl</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>

<servlet-mapping>
<servlet-name>jUnitHostImpl</servlet-name>
<url-pattern>/SoccerApp/junithost/*</url-pattern>
</servlet-mapping>

<!-- Default page to serve -->
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>

</web-app>


And the angular-context.xml file looks like:



<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://ift.tt/GArMu6"
xmlns:xsi="http://ift.tt/ra1lAU" xmlns:context="http://ift.tt/GArMu7"
xmlns:jdbc="http://ift.tt/18IIlo0" xmlns:lang="http://ift.tt/OGfeTY"
xmlns:util="http://ift.tt/OGfeTW" xmlns:tx="http://ift.tt/OGfeU2"
xsi:schemaLocation="http://ift.tt/OGfeTY http://ift.tt/1pFdnVn
http://ift.tt/18IIlo0 http://ift.tt/1l8sNwP
http://ift.tt/GArMu6 http://ift.tt/1jdM0fG
http://ift.tt/OGfeTW http://ift.tt/1aj0W5e http://ift.tt/OGfeU2 http://ift.tt/1cKeJ93 http://ift.tt/GArMu7 http://ift.tt/1bGeTcI">

<context:annotation-config />
<context:component-scan base-package="com.tomholmes.angularjs.phonebook" />

<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/>

<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="url">
<value>jdbc:mysql://localhost:3306/phonebook</value>
</property>
<property name="username">
<value>myusername</value>
</property>
<property name="password">
<value>mypassword</value>
</property>

</bean>

<!-- JNDI DataSource for JEE environments -->
<!-- <jee:jndi-lookup id="dataSource" jndi-name="java:comp/env/jdbc/MyDatabase"/> -->


<!-- Hibernate SessionFactory -->

<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">

<property name="packagesToScan" value="com.tomholmes.angularjs.phonebook.domain" />

<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.cglib.use_reflection_optimizer">true</prop>
</props>
</property>
<property name="dataSource" ref="dataSource" />
</bean>


<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host" value="mail.tomholmes.net" />
<property name="port" value="587" />

<property name="username" value="tom@tomholmes.net" />
<property name="password" value="J0seph1966" />

<property name="javaMailProperties">
<props>
<prop key="mail.smtp.auth">true</prop>
<prop key="mail.smtp.starttls.enable">true</prop>
</props>
</property>
</bean>

<!--
<bean id="sendMailService" class="com.tomholmes.angularjs.phonebook.shared.util.SendEmailService">
<property name="mailSender" ref="mailSender" />
</bean>
-->

<bean id = "transactionManager" class = "org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name = "sessionFactory" ref = "sessionFactory" />
</bean>

<!-- enable the configuration of transactional behavior based on annotations -->
<tx:annotation-driven transaction-manager="transactionManager"/>

</beans>


So, this project compiles under maven just fine. Under eclipse (STS) with the Tomcat 8 engine, it runs fine, and I can go to my app: http://localhost:8080/angularjs-phone-book/ and I can see the index.html just fine, so I know the app is out there.


If I go to:



http://localhost:8080/angularjs-phone-book/users
http://localhost:8080/angularjs-phone-book/users/
http://localhost:8080/angularjs-phone-book/rest/users
http://localhost:8080/angularjs-phone-book/rest/users/


Nothing works, and I get a 404 error that this web-service is not found. But like I said, I know the test works, but I can't see what the exact URL has to be to get there.


I tried deploying the WAR on Tomcat 8 directly, but this web-app won't even start there, supposedly because of logging problems.


If I can provide any more information, please let me know. Any help in finding this would be great. Ultimately I want to have an AngularJS UI on the front-end tied to web-services, and I have to get the web-services working first.


Thanks!


Aucun commentaire:

Enregistrer un commentaire