I am trying to build an application using Spring M

2019-09-11 13:56发布

Here's my web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">


<display-name>lab2</display-name>

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

<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>
<listener>
    <listener-class>
        org.springframework.web.context.ContextLoaderListener
    </listener-class>
</listener>
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/servlet-context.xml</param-value>
</context-param>

Here's my dispatcher-servlet.xml

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-4.3.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd">

<context:component-scan base-package="edu.pratiksanglikar.cmpe281">
</context:component-scan>
<mvc:annotation-driven />

I have a servlet-context.xml as well...

<?xml version="1.0" encoding="UTF-8"?>
 <beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"      xmlns:beans="http://www.springframework.org/schema/beans"
     xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
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
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">

<!-- DispatcherServlet Context: defines this servlet's request-processing 
    infrastructure -->

<!-- Enables the Spring MVC @Controller programming model -->
<annotation-driven />

<!-- Handles HTTP GET requests for /resources/** by efficiently serving 
    up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/" />

<!-- Resolves views selected for rendering by @Controllers to .jsp resources 
    in the /WEB-INF/views directory -->
<!--  <beans:bean
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <beans:property name="prefix" value="/WEB-INF/views/" />
    <beans:property name="suffix" value=".jsp" />
</beans:bean> -->

<beans:bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
    destroy-method="close">
    <beans:property name="driverClassName" value="org.postgresql.Driver" />
    <beans:property name="url"
        value="postgres://dlsqwlarbuvyru:L-t-qHnVAuKp_sIZQ29uKeLCON@ec2-54-83-47-145.compute-1.amazonaws.com:5432/d7is3imb2cn7nm" />
    <beans:property name="username" value="someuser" />
    <beans:property name="password" value="somepassword" />
</beans:bean>

<!-- Hibernate 4 SessionFactory Bean definition -->
<beans:bean id="hibernate4AnnotatedSessionFactory"
    class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <beans:property name="dataSource" ref="dataSource" />
    <beans:property name="annotatedClasses">
        <beans:list>
            <beans:value>edu.pratiksanglikar.cmpe281.hw3.extracredit.beans.Employee</beans:value>
            <beans:value>edu.pratiksanglikar.cmpe281.hw3.extracredit.beans.Project</beans:value>
        </beans:list>
    </beans:property>
    <beans:property name="hibernateProperties">
        <beans:props>
            <beans:prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect
            </beans:prop>
            <beans:prop key="hibernate.show_sql">true</beans:prop>
        </beans:props>
    </beans:property>
</beans:bean>

<beans:bean id="employeeDAO" class="edu.pratiksanglikar.cmpe281.hw3.extracredit.dao.EmployeeDAOImpl">
    <beans:property name="sessionFactory" ref="hibernate4AnnotatedSessionFactory" />
</beans:bean>
<beans:bean id="projectDAO" class="edu.pratiksanglikar.cmpe281.hw3.extracredit.dao.ProjectDAOImpl">
    <beans:property name="sessionFactory" ref="hibernate4AnnotatedSessionFactory" />
</beans:bean>
<beans:bean id="employeeService" class="edu.pratiksanglikar.cmpe281.hw3.extracredit.service.EmployeeServiceImpl">
    <beans:property name="employeeDAO" ref="employeeDAO"></beans:property>
</beans:bean>
<beans:bean id="projectService" class="edu.pratiksanglikar.cmpe281.hw3.extracredit.service.ProjectServiceImpl">
    <beans:property name="projectDAO" ref="projectDAO"></beans:property>
</beans:bean>
<context:component-scan base-package="edu.pratiksanglikar.cmpe281.hw3.extracredit">
    <context:include-filter expression="edu.pratiksanglikar.cmpe281.*" /> 
</context:component-scan>

<tx:annotation-driven transaction-manager="transactionManager"/>

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

Finally I have my controller --

@Controller
@RequestMapping(value = "/employee")
public class EmployeeController {

@Autowired
private EmployeeService employeeService;

/**
 * returns all the employees in the datastore.
 * 
 * @return list of all employees.
 */
@RequestMapping(method = RequestMethod.GET)
public @ResponseBody String getAllEmployees() {
    return convertObjectListToJSONArray(this.employeeService.listEmployees());
}

/**
 * adds an employee in the data-store.
 * 
 * @param employee
 *            employee to add in the data-store.
 * @return request status.
 */
@ResponseStatus(value = HttpStatus.CREATED)
     @RequestMapping(method = RequestMethod.POST, consumes = "application/json", produces = "application/json")
public ResponseEntity<?> addEmployee(UriComponentsBuilder uriCBuilder, @RequestBody Employee employee) {
    Employee e = this.employeeService.getEmployeeById(employee.getId());
    if(null != e) {
        throw new EmployeeConflictException(); 
    }
    this.employeeService.addEmployee(employee);
    UriComponents uriComponents = uriCBuilder.path("/cmpe281Pratik021/rest/employee/{id}")
            .buildAndExpand(employee.getId());
    HttpHeaders headers = new HttpHeaders();
    headers.setLocation(uriComponents.toUri());
    return new ResponseEntity<Void>(headers, HttpStatus.CREATED);
}
}

I tried hitting http://localhost:8080/employee but it says HTTP 404. Ideally it should have returned the list of employees. Please let me know if I'm missing out any details or if there's any error in the configuration file.

1条回答
Juvenile、少年°
2楼-- · 2019-09-11 14:57

A 404 means the container doesn't find a matching URL for the request: "matching" not only means the path must match but also the header parameters. You require "consumes=application/json".. are you sure the client set the header accordingly? (if you test from a "normal" browser it may explain the 404...)

查看更多
登录 后发表回答