我在我的项目中使用Spring MVC,当我在浏览器中输入以下URI时: http://localhost:8080/basic-web-app-0.0.1-SNAPSHOT/rest/accounts/all 我希望得到一份我的帐户清单。 但是,我收到了 404资源未找到错误。 我做错了什么?
我的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">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:mongo-context.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>customers</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>customers</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
customers-servlet.xml如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.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-4.2.xsd">
<!-- <context:component-scan base-package="com.sam.spring.web.rest.mvc" />-->
<context:component-scan base-package="sam.core.services, sam.core.repositories, sam.core.model, sam.rest.mvc"></context:component-scan>
<mvc:resources mapping="/app/**" location="/app/build/"></mvc:resources>
<!-- <context:component-scan base-package="com.sam.spring.web.core.services" />-->
<!-- <bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>
-->
<bean id="jspViewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsps/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<!--<import resource="classpath:WEB-INF/mongo-context.xml"></import>-->
</beans>
帐户控制器类如下:
package sam.rest.mvc;
import java.math.BigInteger;
import java.net.URI;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import sam.core.model.entities.Account;
import sam.core.services.AccountService;
import sam.core.services.exceptions.AccountExistsException;
import sam.rest.exceptions.ConflictException;
import sam.rest.mvc.resources.AccountListResource;
import sam.rest.mvc.resources.AccountResource;
import sam.rest.mvc.resources.asm.AccountListResourceAsm;
import sam.rest.mvc.resources.asm.AccountResourceAsm;
@Controller
@RequestMapping("/rest/accounts")
public class AccountController {
private AccountService accountService;
private static Logger mylogger = LoggerFactory.getLogger(AccountController.class);
/*@Autowired*/
/*public AccountController() {
}*/
@Autowired
public AccountController(AccountService accountService) {
this.accountService = accountService;
}
/*@RequestMapping(method = RequestMethod.GET)
public ResponseEntity<Object> findAllAccounts(@RequestParam(value="email", required = false) String email) {
ArrayList<Account> list = null;
if(email == null) {
Account account1 = new Account();
account1.setEmail("porip@gmail.com");
account1.setPassword("12345");
accountService.createAccount(account1);
list = accountService.findAllAccounts();
} else {
Account account = accountService.findAccount(email);
if(account == null) {
//list = new AccountList(new ArrayList<Account>());;
list = new ArrayList<Account>();
} else {
list = (ArrayList)Arrays.asList(account);
}
}
AccountResource res = new AccountResourceAsm().toResource(list);
//Account res = (Account) list.get(0);
return new ResponseEntity<AccountListResource>(res, HttpStatus.OK);
// return new ResponseEntity<Object>(res.toJSONString(list.get(0)), HttpStatus.OK);
}*/
@RequestMapping(value="/all",method = RequestMethod.GET)
public ResponseEntity<Object> findAllAccounts(@RequestParam(value="email", required = false) String email) {
List<Account> list = null;
if(email == null) {
list = accountService.findAllAccounts();
} else {
Account account = accountService.findAccount(email);
if(account == null) {
list = new ArrayList<Account>();
} else {
list = Arrays.asList(account);
}
}
AccountListResource res = new AccountListResourceAsm().toResource(list);
for(int j=0; j< res.getJsonObjectAccounts().size(); j++) {
mylogger.info(res.getJsonObjectAccounts().get(j));
}
//return new ResponseEntity<AccountListResource>(res, HttpStatus.OK);
return new ResponseEntity<Object>(res.getJsonObjectAccounts().get(0),HttpStatus.OK);
}
@RequestMapping(value="/add",method = RequestMethod.POST)
public ResponseEntity<AccountResource> createAccount(
@RequestBody AccountResource sentAccount) {
System.out.println("Account is talking");
try {
Account createdAccount = accountService.createAccount(sentAccount
.toAccount());
AccountResource res = new AccountResourceAsm()
.toResource(createdAccount);
HttpHeaders headers = new HttpHeaders();
headers.setLocation(URI.create(res.getLink("self").getHref()));
return new ResponseEntity<AccountResource>(res, headers,
HttpStatus.CREATED);
} catch (AccountExistsException exception) {
throw new ConflictException(exception);
}
}
@RequestMapping(value = "/{accountId}", method = RequestMethod.GET)
public ResponseEntity<AccountResource> getAccount(
@PathVariable BigInteger accountId) {
Account account = accountService.findAccount(accountId);
mylogger.info("Works" + account.toString());
if (account != null) {
AccountResource res = new AccountResourceAsm().toResource(account);
return new ResponseEntity<AccountResource>(res, HttpStatus.OK);
} else {
return new ResponseEntity<AccountResource>(HttpStatus.NOT_FOUND);
}
}
}