V
V
Valentine2016-03-22 11:47:51
Java
Valentine, 2016-03-22 11:47:51

How to separate requests to the server API and to its static resources?

Good day everyone! I ran into the problem of separating requests for the server API and static resources, specifically: index.html. That is, the task is as follows: all requests, for example, to: localhost:8080/administrationPanel/tables/highCategory Should return index.html. But if I'm accessing the server API, then it should look like this: localhost:8080/api/getHighCategory . I didn't install any ViewResolvers because I'm giving away static resources (although maybe I'm wrong here), ResourceHandlerRegistry helps me with this :

public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler("/**").addResourceLocations("/");
}

But, if I do something like this:
@RequestMapping(value = "/**", method = RequestMethod.GET)
public String welcome() {       
    return "index.html";
}

Then I will not be able to access my API, because all requests will be intercepted. But if the division is like this:
@RequestMapping(value = "/administrationPanel", method = RequestMethod.GET)
public String welcome() {       
    return "index.html";
}


@RequestMapping(value = "/administrationPanel/**", method = RequestMethod.GET)
public String testWelcome() {

    return "index.html";
}

@RequestMapping(value = "/api/highCategory", method = RequestMethod.GET)
public String apiCategory() {
//code        
    return smth;
}

Then when I go to /administrationPanel everything will be fine, but if I make a request to /administrationPanel/smth , then an HTTP Status 500 - Circular view path error will be returned .
If instead @RequestMapping(value = "/administrationPanel/**"
I do:
@RequestMapping(value = "/administrationPanel/smth"

It will return: HTTP Status 404 - The requested resource is not available.
Although, the servlet manager can also affect this:
@Override
    protected String[] getServletMappings() {
        return new String[]{"/"};
    }

But I don't know how to mess around with it.
How all the same competently to divide requests?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
N
Nick Smith, 2016-03-23
@Waynes

Did so.
Returning static:

@RequestMapping(path = {"film/*", "serial/*"}, method = RequestMethod.GET)
    public String index() {
        return "forward:/index.html";
    }

I refer to the rest:
@RestController
@RequestMapping("/rest/content")
public class ContentController {

    @RequestMapping(method = RequestMethod.GET)
    public HttpStatus searchContent(@Param("name") String name) {
        System.out.println("search for content with param: " + name);
        return HttpStatus.ACCEPTED;
    }

    @RequestMapping(method = RequestMethod.POST)
    public HttpStatus uploadContent(@RequestBody Content content) {
        System.out.println("upload content");
        return upload != null ? HttpStatus.CREATED : HttpStatus.BAD_REQUEST;
    }
}

G
goshan_p, 2016-03-22
@goshan_p

I suspect you need a static proxy. For example nginx

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question