码迷,mamicode.com
首页 > 编程语言 > 详细

Spring RESTful 配置问题

时间:2015-05-02 18:04:58      阅读:484      评论:0      收藏:0      [点我收藏+]

标签:

1.restful模板默认不显示id字段的,可以参考http://tommyziegler.com/how-to-expose-the-resourceid-with-spring-data-rest/

添加id字段。

 

2.spring boot 下jsp 404不能正常显示的问题。参见: http://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#boot-features-jsp-limitations

26.3.4 JSP limitations

When running a Spring Boot application that uses an embedded servlet container (and is packaged as an executable archive), there are some limitations in the JSP support.

  • With Tomcat it should work if you use war packaging, i.e. an executable war will work, and will also be deployable to a standard container (not limited to, but including Tomcat). An executable jar will not work because of a hard coded file pattern in Tomcat.
  • Jetty does not currently work as an embedded container with JSPs.
  • Undertow does not support JSPs.

There is a JSP sample so you can see how to set things up.

 

3.访问不到静态资源.

在servlet的配置文件中,修改

<mvc:resources mapping="/static/**" location="/static"/>

<mvc:resources mapping="/static/**" location="/static/"/>

 

3.上传文件

 在servlet的配置文件中添加

 <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">

程序会陷入循环,一直加载。在pom中添加相应的库

        <!-- Apache Commons FileUpload -->
        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.3.1</version>
        </dependency>

问题解决。

4.Spring Jpa + hibernate + Spring boot 简单配置即刻访问数据库,获取数据, 但是却无法保存。

原因没有添加事物管理,在pom中加入:

        <!--Transaction-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
        </dependency>
        <!--Transaction end-->

在启动时加入资源:

@SpringBootApplication // same as @Configuration @EnableAutoConfiguration @ComponentScan
@ImportResource("classpath:config.xml")
public class Application extends SpringBootServletInitializer{
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(Application.class);
    }
    public static void main(String[] args) {
        ApplicationContext ctx = SpringApplication.run(Application.class, args);

        System.out.println("Let‘s inspect the beans provided by Spring Boot:");

        String[] beanNames = ctx.getBeanDefinitionNames();
        Arrays.sort(beanNames);
        for (String beanName : beanNames) {
            System.out.println(beanName);
        }
    }
}

配置/resource/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:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"
        >
       <!-- ************ JPA configuration *********** -->
       <tx:annotation-driven transaction-manager="transactionManager" />
       <bean id="dataSource"
             class="org.springframework.jdbc.datasource.DriverManagerDataSource">
              <property name="driverClassName" value="com.mysql.jdbc.Driver" />
              <property name="url" value="jdbc:mysql:xxxxx" />
              <property name="username" value="xxx" />
              <property name="password" value="xxx" />
       </bean>
       <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
              <property name="entityManagerFactory" ref="entityManagerFactory" />
       </bean>
       <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
              <property name="dataSource" ref="dataSource" />
              <property name="packagesToScan" value="com.xx" />
              <property name="jpaVendorAdapter">
                     <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                            <property name="showSql" value="true" />
                            <property name="databasePlatform" value="org.hibernate.dialect.MySQLDialect" />
                     </bean>
              </property>
       </bean>
</beans>

 

Spring RESTful 配置问题

标签:

原文地址:http://www.cnblogs.com/CooCoo/p/4468984.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!