Spring RESTful 配置问题

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>

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。