Search code examples
javaspringapachecxfjax-rs

Restricting max file size per resource


I am using Apache-cxf (JAX-RS) coupled with spring framework. I have the following sample code in my beans.xml.

    <jaxrs:server id="services" address="${http.server}">
        <jaxrs:properties>
            <entry key="attachment-max-size" value="1024" />
        </jaxrs:properties>
        <jaxrs:serviceBeans>
            <bean id="mainResource" class="com.abc.rest.api.MainResource">
            <lookup-method name="createEmployeeCollectionResource"
                    bean="employeeCollectionResource" />
            </bean>
        ...
        ...other beans
        ...
        </jaxrs:serviceBeans>
    </jaxrs:server>

I also have the following code

<bean id="employeeCollectionResource"
    class="com.abc.rest.services.EmployeeCollectionResourceImpl">
    <lookup-method name="createNewEmployeeResource" bean="employeeResource" />
</bean>
<bean id="employeeResource" scope="prototype"
    class="com.abc.rest.services.EmployeeResourceImpl">
</bean>

I have set the max file size during upload as 1KB for all the services in general.

How can I restrict the attachment-max-size for few beans in particular? Example - 5MB for few beans and 2MB for few others etc


Solution

  • You will have to organise your endpoints/resources in separate <jaxrs:server > tags with their own settings for attachment size:

    <jaxrs:server id="services">
        <jaxrs:properties>
            <entry key="attachment-max-size" value="1024" />
        </jaxrs:properties>
        ....
    </jaxrs:server>
    
    
    <jaxrs:server id="largeFileServices">
        <jaxrs:properties>
            <entry key="attachment-max-size" value="1000000" />
        </jaxrs:properties>
        ....
    </jaxrs:server>
    

    https://cxf.apache.org/docs/jax-rs-multiparts.html