简体   繁体   中英

Spring-boot: Create referenced uris

Currently, we are building uri references using this straightforward code:

@RestController
@RequestMapping("/fitxers")
public class DocumentController {

    @RequestMapping(value = "/create", method = RequestMethod.GET)
    private ResponseEntity<String> createReferenceURI(String fileName) {

        String uri = ServletUriComponentsBuilder.fromCurrentContextPath()
                .path("/fitxers/download/")
                .path(fileName)
                .toUriString();

        HttpHeaders headersResp = new HttpHeaders();
        headersResp.setLocation(URI.create(uri));
        return new ResponseEntity<String>(uri, headersResp, HttpStatus.CREATED);
    }

    @RequestMapping(value = "/download/{id}", method = RequestMethod.GET)
    void getStreamingFile(HttpServletResponse response, String id) throws IOException {}
}

I'm sure, there have to be another more elegant way to get it.

We are creating spring-boot services.

Any ideas?

Just write a filter, something like this:

@Component
public class HeadersFilter implements Filter {
    @Override
    public void destroy() {

        System.out.println("Filter will be destroyed");
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws
            IOException, ServletException {

        HttpServletResponse httpServletResponse = (HttpServletResponse) response;
        httpServletResponse.setHeader(HttpHeaders.LOCATION, ((RequestFacade) request).getRequestURL().toString());
        chain.doFilter(request, response);     
    }

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

        System.out.println("Filter initialized");
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM