2016-11-07 15 views
0

に達していないことはここでは私のweb.xml私は春の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> 

はここに私のディスパッチャ・サーブレットのだ理由を把握することはできません

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

.XML私もサーブレットの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

アプリケーションをどのように展開しましたか?あなたが戦争を展開した場合、rootアプリケーションとして展開されていない限り、http:// localhost:8080//employee – kuhajeyan

+0

@kuhajeyanのようになりますが、おそらく正しいでしょう。 – Gimby

答えて

0

404は、コンテナが要求に一致するURLを見つけられないことを意味します。「一致」は、パスが一致する必要があるだけでなく、ヘッダーパラメータも意味します。 "consumes = application/json"が必要です。クライアントがヘッダーを適切に設定していることを確認してください。 (もしあなたが "普通"のブラウザからテストすれば、それは404を説明するかもしれません...)

関連する問題