1

EDIT --

This is a stupid question - please ignore it. Going away to learn Java properly.

--

I want to create an API that can be used externally, but I also want to create my own front end to the same API.

If I create an MVC controller & a REST controller in the same app, can I call the REST API from the MVC controller without making an "external" http call, which seems a bit wasteful?

(EDIT - originally linked to another question but it ws wrong. Can't find the right one so removed).

Obviously raises the further question of whether I should or not, but that is, I suppose, subjective. As an aside I'd welcome opinion on that.

EDIT 2 (to add some code in response to chrylis)

So if I create a class like this (example copied from a tutorial somewhere)

public class Greeting {
    private final long id;
    private final String content;

    public Greeting(long id, String content) {
        this.id = id;
        this.content = content;
    }

    public long getId() {
        return id;
    }

    public String getContent() {
        return content;
    }   
}

and a rest controller like this which if called directly from the browser will return json data :

@RestController
public class GreetingController {

    private static final String template = "Hello, %s!";
    private final AtomicLong counter = new AtomicLong();

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

Then I call it from my HTML controller like this :

@Controller
public class HomeController {

    private static final Logger logger = LoggerFactory.getLogger(HomeController.class);
    
    @RequestMapping("/")
    public String home(Model model) {
        
        Greeting gr=new Greeting(1,"Hi");
        logger.info(gr.getContent());
        
        model.addAttribute("modelName",gr.getContent());
        return "home";
    }
}

is that what you mean? It seems to work. The only issue I see is that I'm working on the class directly and not performing the action of the rest api.

Actually, even as I write this I realise this isn't what you meant, I don't think.

1 Answer 1

1

This is actually how I handle my own Web applications. For a Person resource, create a PersonRestController that returns a PersonResource REST representation, and inject the PersonRestController into a PersonHtmlController. The handler methods on the HTML controller call the REST controller and feed the PersonResource (and other relevant attributes) to the model for the HTML template.

Sign up to request clarification or add additional context in comments.

4 Comments

thanks. Would be very grateful if you could you explain or point me to somewhere that would help with that injection bit. I am new to all of this and whilst I am searching I'm not sure I'm using the right terms. Also, I have been reading about testing using MockMvc, which seems to do something similar to what I'm looking for. I assume that would be no good to use a testing method? Just a thought.
It might also be relevant that the REST api is returning a json packaged response.
@beigerac Spring MVC @RestControllers return POJOs, and you can inject controllers and call methods on them just like any other Spring bean.
I added some code to the question in response to this, though I think I've misunderstood.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.