Results 1 to 7 of 7

Thread: Handling Media Types Dynamically

  1. #1
    Join Date
    May 2011
    Location
    Chennai,India
    Posts
    38

    Unhappy Handling Media Types Dynamically

    Hi,
    I want to know if its possible to handle multiple media types using "ByteArrayHttpMessageConverter". My scenario is that, i will not be knowing the media type till i get some document from the server. So, it can be anything (a pdf, a doc, an image etc.,). Is there a way to configure the ByteArrayHttpMessageConverter - media type dynamically?
    Can anyone throw some light on this?
    I have tried giving all possible media types as below,
    Code:
    <bean id="byteArrayMessageConverter"
    
    		class="org.springframework.http.converter.ByteArrayHttpMessageConverter">
    
    		<property name="supportedMediaTypes">
    			<list>
    				
    				<bean class="org.springframework.http.MediaType">
    					<constructor-arg value="application" />
    					<constructor-arg value="pdf" />
    				</bean>
    				<bean class="org.springframework.http.MediaType">
    					<constructor-arg value="text" />
    					<constructor-arg value="plain" />
    				</bean>
    				<bean class="org.springframework.http.MediaType">
    					<constructor-arg value="text" />
    					<constructor-arg value="csv" />
    				</bean>
    				<bean class="org.springframework.http.MediaType">
    					<constructor-arg value="text" />
    					<constructor-arg value="xml" />
    				</bean>
    				<bean class="org.springframework.http.MediaType">
    					<constructor-arg value="application" />
    					<constructor-arg value="msword" />
    				</bean>
    				<bean class="org.springframework.http.MediaType">
    					<constructor-arg value="application" />
    					<constructor-arg value="vnd.ms-excel" />
    				</bean>
                                     </list>
    		</property>
    	</bean>
    But this is not right! it is not working as i expect. Some media types takes precedence over the others




    Thanks in advance!
    Paary

  2. #2
    Join Date
    Nov 2009
    Location
    Montreal, Quebec
    Posts
    398

    Default

    ByteArrayHttpMessageConverter should handle any file type. If you're handler mapping returns a byte[] or a ResponseEntity<byte[]>, then you can set the "Content-Type" on the response based on the mime type of the file being retrieved from the server.

    What do you mean when you say "Some media types takes precedence over the others"?

  3. #3
    Join Date
    Nov 2009
    Location
    Montreal, Quebec
    Posts
    398

    Default

    Here's an example handler mapping from a recent project of mine where I do just that.

    Code:
        @RequestMapping(value = "/previewOther", params = { "r" })
        public ResponseEntity<byte[]> previewOther(@RequestParam("r") File resource, @RequestParam(defaultValue = "1") Integer page) {
        	HttpHeaders headers = new HttpHeaders();
        	headers.set("Content-Type", mediaService.getMimeType(resource));
        	return new ResponseEntity<byte[]>(mediaService.getBytes(resource, page), headers, HttpStatus.OK);
        }

  4. #4
    Join Date
    May 2011
    Location
    Chennai,India
    Posts
    38

    Default

    Thanks for your reply!
    First of all is the bean declaration for byte array message converter is correct?
    Because, if i set like this, if i get an image doc, it tries to open with excel
    and sometimes it opens as zip file.
    And thanks for suggesting me this Will surely try this way!
    Thanks for your help

  5. #5
    Join Date
    Nov 2009
    Location
    Montreal, Quebec
    Posts
    398

    Default

    According to the documentation at http://static.springsource.org/sprin...-config-enable, if you're using <mvc:annotation-driven> in your MVC config, a ByteArrayHttpMessageConverter is registered for you automatically.

  6. #6
    Join Date
    May 2011
    Location
    Chennai,India
    Posts
    38

    Default

    I believe there is a gap in understanding. I want to implement this in SI. I should have posted this in integration
    Thanks alot for your help
    I will move this post to integration.

  7. #7
    Join Date
    May 2011
    Location
    Chennai,India
    Posts
    38

    Default

    i have posted the same question with some more details in the integration forum.The link to the same is,
    http://forum.springsource.org/showth...es-dynamically

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •