HTTP 404错误-原始服务器找不到目标资源的当前表示形式,或者不愿意透露存在的一种表示形式

时间:2019-02-12 07:22:20

标签: mysql hibernate rest spring-mvc

我是spring_mvc的新手,我正尝试使用spring_mvc,hibernate和mysql构建Restful spi服务。我检查了很多源,但无法知道我的代码中的错误。请帮助我找到问题。 我分享了以下文件 控制器类:

@Controller
public class ViewController {
    @RequestMapping("viewaliens")
    public String viewProducts(ModelMap model) {
        model.addAttribute("message", "Framework!"+"ohho");
        return "view/viewaliens";
    }
    @RequestMapping("addaliens")
    public String addProducts() {

        return "view/addaliens";
    }

}

在浏览器中输入url时:/ addaliens或/ viewaliens ...,然后分别调用html文件,并从那里将发布请求发送到/ aliens,该请求由以下restcontroller类处理。到目前为止,我只尝试发布请求..即其他工作。     @RestController

public class AlienServiceControl{




    private static Map<Integer, Alien> alienRepo = new HashMap<Integer, Alien>();

    //-------------------------------------POST------------------------------------------------------
        @RequestMapping(value = "aliens", method = RequestMethod.POST)
        public ResponseEntity<Object> createProduct (@RequestBody Alien alien){
            Configuration configuration =new Configuration().addAnnotatedClass(Alien.class).configure();
            StandardServiceRegistryBuilder ssrb = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties());
            SessionFactory session = configuration.buildSessionFactory(ssrb.build());

            alienRepo.put(alien.getAid(), alien);
            Session s= session.openSession();
            Transaction tx = null;
            Integer alienid=null;

            try {
                tx = s.beginTransaction();
                Alien aln = new Alien(alien.getAid(), alien.getAname(), alien.getAcolour());
                alienid=(Integer)s.save(aln);
                tx.commit();
            } catch (HibernateException e) {
                if (tx!=null) tx.rollback();
                e.printStackTrace();
            } finally {
                s.close();
            }
            return new ResponseEntity("Product is created successfully", HttpStatus.CREATED);
        }
//-------------------------------------GET----------------------------------------------------------------
        @RequestMapping(value = "aliens")
        public ResponseEntity<Object> getProduct () {
            return new ResponseEntity(alienRepo.values(), HttpStatus.OK);
        }

Alien.java:(Alien类)

@Entity
@Table(name = "al_details")
public class Alien{
    @Id
    public int aid;
    public String aname;
    public String acolour;

    public Alien(int aid, String aname, String acolour) {
        this.aid = aid;
        this.aname = aname;
        this.acolour = acolour;
    }

    public Alien() {

    }

    public String getAname() {
        return aname;
    }

    public void setAname(String aname) {
        this.aname = aname;
    }

    public int getAid() {
        return aid;
    }

    public void setAid(int aid) {
        this.aid = aid;
    }

    public String getAcolour() {
        return acolour;
    }

    public void setAcolour(String acolour) {
        this.acolour = acolour;
    }
}

web.xml:

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <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>
</web-app>

dispatcher-servlet.xml

<context:component-scan base-package = "Controller" />
    <mvc:annotation-driven/>
    <context:annotation-config/>
    <!--<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" />
    <bean name="/addaliens" class="springfm.ViewController" />
    -->

    <bean class = "org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name = "prefix" value = "/WEB-INF/" />
        <property name = "suffix" value = ".html" />
    </bean>

addaliens.html:

<head>
    <meta charset = "ISO-8859-1" />
    <title>Add Products</title>
    <script src = "https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

    <script TYPE="text/javascript">
        $(document).ready(function() {
            $("button").click(function() {
                var productmodel = {
                    id : document.getElementById("id").innerHTML;
                    name : document.getElementById("name").innerHTML;
                    colour:document.getElementById("colour").innerHTML;
                };
                var requestJSON = JSON.stringify(productmodel);
                $.ajax({
                    type : "POST",
                    url : "http://localhost:8080/aliens",
                    headers : {
                        "Content-Type" : "application/json"
                    },
                    data : requestJSON,
                    success : function(data) {
                        alert(data);
                    },
                    error : function(data) {
                    }
                });
            });
        });
    </script>
</head>

<body>
<form action="/aliens" method="post">
    ID<input id="id" type="text">
    NAME<input id="name" type="text">
    COLOUR<input id="colour" type="text">
    <button>Click</button>
</form>

休眠映射文件:

<hibernate-mapping>
    <class name = "springfrm.Alien" table = "al_details">

        <meta attribute = "class-description">
            This class contains the employee detail.
        </meta>

        <id name = "aid" type = "int" column = "aid">
            <generator class="native"/>
        </id>

        <property name = "aname" column = "aname" type = "string"/>
        <property name = "acolour" column = "acolour" type = "string"/>


    </class>
</hibernate-mapping>

pom.xml:由于无法正常工作,我从沮丧中添加了太多依赖项。:)

<groupId>new_pr1</groupId>
    <artifactId>newproject</artifactId>
    <version>1.0-SNAPSHOT</version>
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.1.4.RELEASE</version>
            <exclusions>
                <!-- Exclude Commons Logging in favor of SLF4j -->
                <exclusion>
                    <groupId>commons-logging</groupId>
                    <artifactId>commons-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>5.4.1.Final</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>5.4.1.Final</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>5.1.4.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.1.4.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>5.1.4.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>commons-dbcp</groupId>
            <artifactId>commons-dbcp</artifactId>
            <version>1.4</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>1.9.2</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.8.0-beta2</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>jcl-over-slf4j</artifactId>
            <version>1.8.0-beta2</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.8.0-beta2</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.15</version>
            <exclusions>
                <exclusion>
                    <groupId>javax.mail</groupId>
                    <artifactId>mail</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>javax.jms</groupId>
                    <artifactId>jms</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>com.sun.jdmk</groupId>
                    <artifactId>jmxtools</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>com.sun.jmx</groupId>
                    <artifactId>jmxri</artifactId>
                </exclusion>
            </exclusions>
            <scope>runtime</scope>
        </dependency>

        <!-- @Inject -->
        <dependency>
            <groupId>javax.inject</groupId>
            <artifactId>javax.inject</artifactId>
            <version>1</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>5.1.4.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>5.1.4.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.13</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp.jstl</groupId>
            <artifactId>javax.servlet.jsp.jstl-api</artifactId>
            <version>1.2.2</version>
        </dependency>
        <dependency>
            <groupId>taglibs</groupId>
            <artifactId>standard</artifactId>
            <version>1.1.2</version>
        </dependency>
        <!-- Servlet Dependency -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>4.0.0-b05</version>
            <scope>provided</scope>
        </dependency>
        <!-- JSP Dependency -->
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>javax.servlet.jsp-api</artifactId>
            <version>2.3.3</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.7</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>3.0-alpha-1</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <warSourceDirectory>web/WEB-INF/</warSourceDirectory>

                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat9-maven-plugin</artifactId>
                <version>9</version>

            </plugin>
        </plugins>
    </build>

0 个答案:

没有答案