0

The goal is to create a mock server for testing the Rest. I created the controller, made the mapping, and entities.

@RestController
public class GreetingController {
    private static final String template = "Hello, %s!";
    private final AtomicLong counter = new AtomicLong();

    @RequestMapping("/greeting")
    public GreetingResponse greeting(@RequestParam(value="name", defaultValue="World")
            String name) {
        return new GreetingResponse(counter.incrementAndGet(),
            String.format(template, name));
    }
}

How can I make a proxy so that all requests that are not equal to "/greeting" are passed to the other server unchanged and the response that the client would receive through our application?

Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132
DmitryJS
  • 186
  • 2
  • 12
  • Does [this answer](https://stackoverflow.com/questions/36594644/spring-controller-to-handle-all-requests-not-matched-by-other-controllers) help? – Andy Brown Apr 11 '18 at 15:24
  • Thanks Andy. Now I understand at the expense of mapping. But it is not yet possible to understand how to avoid redirection (302). A third party does not expect this status and may not understand what is happening, although it would be very simple. – DmitryJS Apr 11 '18 at 16:58
  • The accepted answer in the link returns a redirect. Don't do that. Change the method to have HttpServletRequest and HttpServletResponse parameters and use those to generate a proxy call and to pass back the response. There are many subtleties to doing this properly but for a mock server I think you can achieve what you want with only a little code. – Andy Brown Apr 12 '18 at 12:33
  • Andy thanks for answer. Everything worked out. I used okHttpClient. I already managed to implement a proxy for simple queries. But I understood the idea and this is important. Your answer is a decision – DmitryJS Apr 13 '18 at 18:58

1 Answers1

0

Maybe response.sendRedirect? Or so

Jan Schumacher
  • 309
  • 2
  • 11
  • Thanks for the answer, I thought about it, but it seems to me that this is the wrong way. We will give the status of 302, but what I want to replace may not understand it. It will be too artificial. But this would greatly simplify the task – DmitryJS Apr 11 '18 at 16:52