-2

I have two seperate projects, a projectApi (spring backend) and a projectUi (angular frontent). I'm using maven-resource-plugin to combine them into one jar for production. When I start the spring server, the connection between those two modules works fine.

Now I would like to customize the backend url path, so that a request like 'http://localhost:8088/login' looks like 'http://localhost:8088 /api/v1/login'.

I was able to do so, by adding the following entry to application.properties: spring.mvc.servlet-path=/api/v1 and modifying the base url, for calls from the ui to the api.

Since that change I'm getting a whitelabel error calling the ui (localhost:8088). After some search, I tried to implement WebMvcConfigurer but it did not work for me. This is the reference stackoverflow link.

// Application.java

@SpringBootApplication
@EnableJpaRepositories
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}


// UserRestService.java

@RestController
@RequestMapping("/user")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public class UserRestService extends AbstractRestService {
  ...
  @PostMapping("/login")
    public String login(@RequestBody User user) throws CustomException {
        return Response.ok(userService.loginUser(user, response));
    }
}



// application.properties

server.port=8088

// without that entry the post request works fine -> localhost:8088/user/login
// adding that entry and trying to call: localhost:8088/api/v1/user/login i get whitelabel error
spring.mvc.servlet.path=/api/v1
  • Cant help without seeing your code . – Shubham Dixit Apr 17 '21 at 21:41
  • @Shubh I added you some code, not sure what exactly you want / need to see. The only thing I changed was this line in the application.properties file and the base-url that the frontent uses. Maybe you know completely different way, how I can reach my goal. I'd like to reach the frontent with the address localhost:8088/... and the backend with the address localhost:8088/api/v1/... Thanks – pecigap736 Apr 17 '21 at 22:04

2 Answers2

0

Try to add "/api/v1/" to your controller otherwise all your controllers will be prefixed by that path and you will not be able to provide other versions withing the same application.

zahid med
  • 61
  • 5
0

I would prefer to program to an interface. This helps in making better use of IOC. Add the URI prefix (/api/v1/) to the interface like below. It will append this prefix to all methods offered by the interface.

// Interface for UserRestService with URI prefix mentioned using Request mapping annotation
@RequestMapping(value = "/api/certs/", produces = { "application/json" },consumes = { "application/json" })
public interface IUserRestService {
  String login(User user) throws CustomException;
}

//UserRestService Class 
@RestController
@RequestMapping("/user", method = RequestMethod.POST)
public class UserRestService extends AbstractRestService implements IUserRestService{
  ...
  @PostMapping("/login")
    public String login(@RequestBody User user) throws CustomException {
        return Response.ok(userService.loginUser(user, response));
    }
}
Janani
  • 71
  • 4