Announcement Announcement Module
Collapse
No announcement yet.
Handling Media Types Dynamically Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    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"?

    Comment


    • #3
      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);
          }

      Comment


      • #4
        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

        Comment


        • #5
          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.

          Comment


          • #6
            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.

            Comment


            • #7
              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

              Comment

              Working...
              X