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>

 5.配置ueditor后出现"未找到上传数据"错误,网上查找之后据说是springmvc中的上传组件与之不兼容,参见https://github.com/fex-team/ueditor/issues/827

重写了 BinaryUploader.save问题解决。

6.在spring中引入文件系统中的资源,目前用于管理上传的图片。在配置文件中加入:

<mvc:resources mapping="/resource/**" location="file:/home/www/upload/resource/"/>

7.在controller中添加Pageable参数,但出现无法实例化的问题。

在配置文件中添加如下配置,问题解决。

<mvc:annotation-driven>
    <mvc:argument-resolvers>
        <bean class="org.springframework.data.web.PageableHandlerMethodArgumentResolver" />
    </mvc:argument-resolvers>
</mvc:annotation-driven>

8.在controller中直接返回page数据,jsp页面的foreach不能解析,只能先转换为List.

9.启用hibernate的默认主键生成策略,可以添加如下注解:

    @Id
    @GeneratedValue(generator = "ly_uuid")
    @GenericGenerator(name="ly_uuid",strategy = "uuid2")
    private String id;                        //商品id

但是为了能够先获取到商品id,因此采用自定义的uuid生成器,使得在插入数据库之前便可以获取id。

10.在RestController中如下代码在执行时会发生getOutputStream已经调用的错误

    @RequestMapping(value = "/articles/",method = RequestMethod.GET)
    public ResponseState list(@RequestParam(required = false) String typeid, Pageable pageable ){

        List<Article> list;
        if(typeid!=null){
            list = articleRepository.findByPhaseAndDelAndTypeid(Article.Phase.COMPLETE.ordinal(), 0, typeid, pageable).getContent();
        }else{
            list = articleRepository.findByPhaseAndDel(Article.Phase.COMPLETE.ordinal(),0,pageable).getContent();
        }
        return new ResponseState(true,"suc",list);
    }

出错原因是因为Article内部还有一个List<Comment>是通过jpa获取的,给comment加上@Transient注解后问题解决.

posted @ 2015-05-02 16:21  phk52  阅读(551)  评论(0编辑  收藏  举报