2016-11-07 22 views
0

這裏是我的web.xml我想使用Spring MVC來構建應用程序和想不通爲什麼HTTP請求沒有達到我的春節控制器

<?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> 

這裏是我的調度員的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 /> 

我有一個servlet-context.xml中,以及...

<?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:[email protected]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> 

最後,我有我的控制器 -

@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); 
} 
} 

我試着打http://localhost:8080/employee但它說,HTTP 404 理想情況下,應返回的員工列表。 如果我錯過了任何詳細信息或配置文件中有任何錯誤,請讓我知道。

+0

日誌消息顯示什麼?訪問'/ employee'時你有任何錯誤嗎?並檢查'/ employee'和addEmployee是否被正確映射和註冊(檢查服務器啓動日誌)。 – Lucky

+3

你是如何部署應用程序的?如果你部署了一場戰爭,它應該像http:// localhost:8080/<你的應用程序ctx> /僱員 – kuhajeyan

+0

@kuhajeyan,除非作爲根應用程序部署,但你可能是正確的。 – Gimby

回答

0

404意味着容器沒有爲請求找到匹配的URL:「匹配」不僅意味着路徑必須匹配,而且意味着頭部參數。 你需要「消費=應用程序/ JSON」..你確定客戶端相應地設置標題? (如果您從「普通」瀏覽器進行測試,它可能會解釋404 ...)

相關問題