Java 请求的资源不可用404错误

Java 请求的资源不可用404错误,java,spring,rest,spring-mvc,Java,Spring,Rest,Spring Mvc,我在项目中使用Spring MVC,当我在浏览器中输入以下URI时: 我希望能得到我的账户清单。 但是,我得到一个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:s

我在项目中使用Spring MVC,当我在浏览器中输入以下URI时: 我希望能得到我的账户清单。 但是,我得到一个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>
  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);
        }
    }


}

上下文配置位置
类路径:mongo-context.xml
org.springframework.web.context.ContextLoaderListener
客户
org.springframework.web.servlet.DispatcherServlet
1.
客户
/
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>

帐户控制器类如下所示:

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


}
包sam.rest.mvc;
导入java.math.biginger;
导入java.net.URI;
导入java.util.ArrayList;
导入java.util.array;
导入java.util.List;
导入org.slf4j.Logger;
导入org.slf4j.LoggerFactory;
导入org.springframework.beans.factory.annotation.Autowired;
导入org.springframework.http.HttpHeaders;
导入org.springframework.http.HttpStatus;
导入org.springframework.http.ResponseEntity;
导入org.springframework.stereotype.Controller;
导入org.springframework.web.bind.annotation.PathVariable;
导入org.springframework.web.bind.annotation.RequestBody;
导入org.springframework.web.bind.annotation.RequestMapping;
导入org.springframework.web.bind.annotation.RequestMethod;
导入org.springframework.web.bind.annotation.RequestParam;
导入sam.core.model.entities.Account;
导入sam.core.services.AccountService;
导入sam.core.services.exceptions.AccountExistsException;
导入sam.rest.exceptions.ConflictException;
导入sam.rest.mvc.resources.AccountListResource;
导入sam.rest.mvc.resources.AccountResource;
导入sam.rest.mvc.resources.asm.AccountListResourceAsm;
导入sam.rest.mvc.resources.asm.AccountResourceAsm;
@控制器
@请求映射(“/rest/accounts”)
公共类帐户控制器{
私人帐户服务;
私有静态记录器mylogger=LoggerFactory.getLogger(AccountController.class);
/*@自动连线*/
/*公共账户控制员(){
}*/
@自动连线
公共会计控制员(会计服务会计服务){
this.accountService=accountService;
}
/*@RequestMapping(method=RequestMethod.GET)
公共响应findallaAccounts(@RequestParam(value=“email”,required=false)字符串电子邮件){
ArrayList list=null;
如果(电子邮件==null){
账户1=新账户();
account1.setEmail(“porip@gmail.com");
账户1.设置密码(“12345”);
accountService.createAccount(account1);
list=accountService.findAllAccounts();
}否则{
Account=accountService.findAccount(电子邮件);
如果(帐户==null){
//list=新的AccountList(newarraylist());;
列表=新的ArrayList();
}否则{
list=(ArrayList)Arrays.asList(account);
}
}
AccountResource res=新AccountResourceAsm().toResource(列表);
//帐户res=(帐户)列表。获取(0);
返回新的响应状态(res,HttpStatus.OK);
//返回新的ResponseEntity(res.toJSONString(list.get(0)),HttpStatus.OK);
}*/
@RequestMapping(value=“/all”,method=RequestMethod.GET)
公共响应findallaAccounts(@RequestParam(value=“email”,required=false)字符串电子邮件){
List=null;
如果(电子邮件==null){
list=accountService.findAllAccounts();
}否则{
Account=accountService.findAccount(电子邮件);
如果(帐户==null){
列表=新的ArrayList();
}否则{
list=Arrays.asList(account);
}
}
AccountListResource res=新AccountListResourceAsm().toResource(列表);
对于(int j=0;j
您的servlet正在侦听
/
-您能确认上下文是否正确吗?(即,转到tomcat的管理控制台,查看应用程序的部署位置)-管理控制台通常位于
http://localhost:8080/admin
tomcat不再支持管理员web控制台。那我该怎么办?你使用的是什么版本的tomcat?你明白了吗