Tag Archives: spring

spring AbstractExcelView and IE

Questions: I am using spring’s ContentNegotiatingViewResolver in order to export data to excel files. This works perfectly in Chrome and FireFox, however in IE instead of downloading the file Explorer displays gibberish in the browser. This my config snippet: <bean class=”org.springframework.web.servlet.view.ContentNegotiatingViewResolver” p:order=”1″> <property name=”mediaTypes”> <map> <entry key=”xml” value=”application/xml” /> <entry key=”txt” value=”text/plain” /> <entry key=”xls”… Read More »

downloading file from Spring Rest Controller

Questions: I am trying to download a file through Spring REST Controller. Below is my code – @RequestMapping(value=”/aaa”,method=RequestMethod.GET,produces=MediaType.APPLICATION_OCTATE_STREAM_VALUE) public ResponseEntity<byte[]> testMethod(@RequestParam(“test”) String test) { HttpHeaders responseHeaders = new HttpHeaders(); responseHeaders.setContentDispositionFromData(“attachment”,”testExcel.xlsx”); responseHeaders.setContentType(MediaType.APPLICATION_OCTET_STREAM); File file = new File(“C:\\testExcel.xlsx”); Path path = Paths.get(file.getAbsolutePath()); ByteArrayResource resource = new ByteArrayResource(Files.readAllbytes(path)); return new ResposeEntity<byte[]>(resource.getByteArray(),responseHeaders,HttpStatus.OK); } This is called in a button… Read More »

Unable to upload excel file using spring + apache-poi

Questions: When I’m trying to upload excel file (.xls/.xlsx), then I’m getting following exception : org.apache.poi.poifs.filesystem.OfficeXmlFileException: The supplied data appears to be in the Office 2007+ XML. You are calling the part of POI that deals with OLE2 Office Documents. You need to call a different part of POI to process this data (eg XSSF… Read More »

Spring MVC, Excel file download, corrupts file

Questions: I’am working on an excel export functionality in one of my webapps. I set up a little test case and got the download working, but the xlsx file is corrupted and don’t know what else I could try. If I write the excel to file it opens without problem, so the error must occure… Read More »

Spring annotation-based DI vs xml configuration?

Questions: Recently in our team we started discussing using spring annotations in code to define spring dependencies. Currently we are using context.xml to define our dependencies. Would you give me some clues for either approach, and when one is better to be used? Edit: I know this seems a duplicate question to a more-general one,… Read More »

JSON Java 8 LocalDateTime format in Spring Boot

Questions: I’m having a small problem with formatting a Java 8 LocalDateTime in my Spring Boot Application. With ‘normal’ dates I have no problem, but the LocalDateTime fields are converted to the following: “startDate” : { “year” : 2010, “month” : “JANUARY”, “dayOfMonth” : 1, “dayOfWeek” : “FRIDAY”, “dayOfYear” : 1, “monthValue” : 1, “hour”… Read More »

How to find all controllers in Spring MVC?

Questions: To provide some runtime generated API documentation I want to iterate over all Spring MVC controllers. All controllers are annotated with the Spring @Controller annotation. Currently I do it like this: for (final Object bean: this.context.getBeansWithAnnotation( Controller.class).values()) { …Generate controller documentation for the bean… } But the first call of this code is EXTREMELY… Read More »

How to customise the Jackson JSON mapper implicitly used by Spring Boot?

Questions: I’m using Spring Boot (1.2.1), in a similar fashion as in their Building a RESTful Web Service tutorial: @RestController public class EventController { @RequestMapping(“/events/all”) EventList events() { return proxyService.getAllEvents(); } } So above, Spring MVC implicitly uses Jackson for serialising my EventList object into JSON. But I want to do some simple customisations to… Read More »

Is it possible to use raw SQL within a Spring Repository

Questions: I need to use raw SQL within a Spring Data Repository, is this possible? Everything I see around @Query is always entity based. Answers: The @Query annotation allows to execute native queries by setting the nativeQuery flag to true. Quote from Spring Data JPA reference docs. Also, see this section on how to do… Read More »