邮差中的404错误 - 春季休息

时间:2016-11-02 10:20:34

标签: rest spring-mvc postman spring-web

我在Postman中测试我的REST Spring WebService时遇到404错误。

有人可以提出解决方案吗?

我的代码:

src/main/resources/spring/application-config.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"
    xsi:schemaLocation="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">

    <!-- Uncomment and add your base-package here:
         <context:component-scan
            base-package="org.springframework.samples.service"/>  -->

</beans>

-------------------------------------------------------------------------------------------------------------------------------------------

src/main/resources/logback.xml

-------------------------------------------------------------------------------------------------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<!-- configuration file for LogBack (slf4J implementation)
See here for more details: http://gordondickens.com/wordpress/2013/03/27/sawing-through-the-java-loggers/ -->
<configuration scan="true" scanPeriod="30 seconds">

    <contextListener class="ch.qos.logback.classic.jul.LevelChangePropagator">
        <resetJUL>true</resetJUL>
    </contextListener>

    <!-- To enable JMX Management -->
    <jmxConfigurator/>

    <appender name="console" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%-5level %logger{0} - %msg%n</pattern>
        </encoder>
    </appender>

    <!--<logger name="org.hibernate" level="debug"/> -->
   <!-- Uncomment and add your logger here:
    <logger name="org.springframework.samples.service.service" level="debug"/> -->

    <root level="info">
        <appender-ref ref="console"/>
    </root>
</configuration>

---------------------------------------------------------------------------------------------------------------------------------------------
src/main/webapp/WEB-INF/mvc-config.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:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
    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">

    <!-- Uncomment and your base-package here:
         <context:component-scan
            base-package="org.springframework.samples.web"/>  -->


    <mvc:annotation-driven />

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <!-- Example: a logical view name of 'showMessage' is mapped to '/WEB-INF/jsp/showMessage.jsp' -->
            <property name="prefix" value="/WEB-INF/view/"/>
            <property name="suffix" value=".jsp"/>
    </bean>

</beans>
---------------------------------------------------------------------------------------------------------------------------------------------
src/main/webapp/WEB-INF/web.xml
---------------------------------------------------------------------------------------------------------------------------------------------
<?xml version="1.0" encoding="ISO-8859-1"?>
<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_2_5.xsd"
         id="WebApp_ID" version="2.5">

    <display-name>ManageUserRoles</display-name>

   <!--
        - Location of the XML file that defines the root application context.
        - Applied by ContextLoaderListener.
    -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring/application-config.xml</param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>


    <!--
        - Servlet that dispatches request to registered handlers (Controller implementations).
    -->
    <servlet>
        <servlet-name>dispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/mvc-config.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>dispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>

//////////////////////////控制器//////////////////// ///////////

package com.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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.ResponseBody;

import com.model.Role;

@Controller
public class RoleController {

    private static final Logger logger = LoggerFactory.getLogger(RoleController.class);

    Map<String, Role> roleData = new HashMap<String, Role>();

    @RequestMapping(value = "/role/{role_name}", method = RequestMethod.GET,headers="Accept=application/json")
    public @ResponseBody Role findRole(@PathVariable("role_name") String role_name)
    {
        logger.info("Get a single role with name="+role_name);
        return roleData.get(role_name);
    }

    @RequestMapping(value = "/roles", method = RequestMethod.GET)  
    public @ResponseBody List<Role> getAllRoles()
    {
        logger.info("Start getAllRoles.");
        List<Role> list_roles = new ArrayList<Role>();
        list_roles = createRoleList();
        return list_roles;

    }

    public List<Role> createRoleList()
    {
        Role role1 = new Role("NP Sherm Test", "NP Test", "Active");
        Role role2 = new Role("Sherm Employee Test", "Employee access test role", "Active");
        Role role3 = new Role("Sherm Manager Test", "Manager test user role", "Active");

        List<Role> list_roles = new ArrayList<Role>();
        list_roles.add(role1);
        list_roles.add(role2);
        list_roles.add(role3);

        return list_roles;

    }
    @RequestMapping(value="/roles/new/",method=RequestMethod.POST,headers="Accept=application/json")
    public @ResponseBody Role addRole(@RequestBody Role role)
    {
        logger.info("Start Add Role");
        Role roleGet = roleData.put(role.getRole_name(), role);
        return roleGet;
    }
    @RequestMapping(value="/roles/new/",method=RequestMethod.PUT,headers="Accept=application/json")
    public @ResponseBody Role editRole(@RequestBody Role role)
    {
      Role role_update = new Role();
      role.setRole_name(role.getRole_name());
      role.setRole_desc(role.getRole_desc());
      role.setRole_status(role.getRole_status());
      return role_update;
    }

}

/////////////////////////////////////模型///////// //////////////////////

package com.model;

public class Role {

    private String role_name;
    private String role_desc;
    private String role_status;

    public Role(){}



    public Role(String role_name, String role_desc, String role_status) {
        super();
        this.role_name = role_name;
        this.role_desc = role_desc;
        this.role_status = role_status;
    }



    public String getRole_name() {
        return role_name;
    }
    public void setRole_name(String role_name) {
        this.role_name = role_name;
    }
    public String getRole_desc() {
        return role_desc;
    }
    public void setRole_desc(String role_desc) {
        this.role_desc = role_desc;
    }
    public String getRole_status() {
        return role_status;
    }
    public void setRole_status(String role_status) {
        this.role_status = role_status;
    }


}

//////////////////////////////的pom.xml ////////////// ////////////////////

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>org.springframework.samples.service.service</groupId>
  <artifactId>ManageUserRoles</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>

    <properties>

        <!-- Generic properties -->
        <java.version>1.6</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

        <!-- Web -->
        <jsp.version>2.2</jsp.version>
        <jstl.version>1.2</jstl.version>
        <servlet.version>2.5</servlet.version>


        <!-- Spring -->
        <spring-framework.version>3.2.3.RELEASE</spring-framework.version>

        <!-- Hibernate / JPA -->
        <hibernate.version>4.2.1.Final</hibernate.version>

        <!-- Logging -->
        <logback.version>1.0.13</logback.version>
        <slf4j.version>1.7.5</slf4j.version>

        <!-- Test -->
        <junit.version>4.11</junit.version>

    </properties>

    <dependencies>

        <!-- Spring MVC -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${spring-framework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring-framework.version}</version>
        </dependency>

        <!-- Other Web dependencies -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>${jstl.version}</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>${servlet.version}</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
            <version>${jsp.version}</version>
            <scope>provided</scope>
        </dependency>

        <!-- Spring and Transactions -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>${spring-framework.version}</version>
        </dependency>

        <!-- Logging with SLF4J & LogBack -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>${slf4j.version}</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>${logback.version}</version>
            <scope>runtime</scope>
        </dependency>

        <!-- Hibernate -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>${hibernate.version}</version>
        </dependency>


        <!-- Test Artifacts -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>${spring-framework.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>

    </dependencies> 
</project>

1 个答案:

答案 0 :(得分:0)

由于您没有任何特定的应用程序上下文,问题在于当前的URL映射,即URI ManageUserRoles未映射到控制器,因此更改代码如下:

@Controller("ManageUserRoles")
public class RoleController {

// Add all you existing handler methods

}
  

在更新1之后:

此外,您的组件在mvc-config.xml中不正确,因为无法检测到哪个控制器:

base-package="org.springframework.samples.web"将其更改为base-package="com.controller"