Changing OpenAPI's autogenerated HomeController - spring

I have a Open API 3.0 swagger doc. I am using Spring's OpenAPI autogen plugin to generate the API controllers and Delegates.
There is a HomeController that gets autogenerated.
/**
* Home redirection to OpenAPI api documentation
*/
#Controller
public class HomeController {
#RequestMapping("/")
public String index() {
return "redirect:swagger-ui.html";
}
}
Is there a way I can change base path in my swagger documentation to create a HomeController similar to this:
#Controller
public class HomeController {
#RequestMapping("/")
public String index() {
return "redirect:/rest/swagger-ui.html";
}
}
My stack:
Spring Boot: 2.1.1
Openapi: 3.0.0

Look at these:
Change location to call swagger-ui in Spring
How to change swagger-ui.html default path
My raw solution:
server.servlet.context-path=/rest
but in this way all the paths start whit /rest
EDIT
can this be fine?
#Controller
public class HomeController {
#RequestMapping("/rest")
public String index() {
return "redirect:/swagger-ui.html";
}
}

Related

Can you use jsp for your front end while your backend routes are restful in Spring Boot?

#RestController
public class IndexController {
#RequestMapping("/")
public String indexRoute() {
return JSON HERE;
}
Suppose the above is my backend route, is there any way with jsp in which i can display that?
I really dont want to use React to embed it.
Controller:
#Controller
public class MyController {
#GetMapping("/json")
#ResponseBody
public MyResource idexJson() {
...
return res;
}
#GetMapping("/jsp")
public String input(final Model model) {
...
model.addAttribute(res);
return "index";
}
}
Additionally, you may possibly need additional configuration. For example:
properties:
spring.mvc.view.prefix= /WEB-INF/view/
spring.mvc.view.suffix= .jsp
dependencies:
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
Example code
Yes you can annotate your class with #RestController to tell Spring that it's methods will return a HTTP response body (in JSON format for example)
And you can annotate an other class with #Controller to tell Spring that it's methods will return a view name (like a JSP file name for example)

Controller or RestController

i'm new to jEE, and this is my first jEE code using spring. The code bellow is working fine. He just print the string index when i go to my localhost; and otherwise he print handling error.
My question is: Why this code isn't working anymore if I use #Controller instead of #RestController
I can't find any simple explanation in the docs from spring and I was hoping someone could explain this.
I have the feelings that a controller alone can't work without something like thymeleaf (I know if I were using thymeleaf the string index would be replaced by the index page from the ressources folder) where a RestController might be returning data as xml or json or something else.
Thanks
#RestController
public class HelloController implements ErrorController {
#RequestMapping("/")
public String index() {
return "index";
}
#RequestMapping("/error")
public String error() {
return "gestion erreur";
}
#Override
public String getErrorPath() {
return "/error";
}
}
The job of #Controller is to create a Map of model object and find a view but #RestController simply return the object and object data is directly written into HTTP response as JSON or XML.
The #Controller is a common annotation which is used to mark a class as Spring MVC Controller while #RestController is a special controller used in RESTFul web services and the equivalent of #Controller + #ResponseBody.
If you want the same functionality of #RestController without using it you can use #Controller and #ResponseBody.
#Controller
public class HelloController{
#RequestMapping("/")
#ResponseBody
public String index() {
return "index";
}
}

#RequestMapping not working in Spring Boot

Controller class.
#RestController
#RequestMapping("/check")
public class Controller {
public String index(){
return "sdfksdjfkjkUshshdfisdfsdkasjdfjkasjdfkjakl:";
}
Application class
#SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
Added all the necessary dependency when running the application shows the
http://localhost:8081/demo/
Hello World
of index.xml
When I change to http://localhost:8081/check/ it gives
HTTP Status 404 – Not Found
Type Status Report
Message /check
Description The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.
How can I understand the flow of Spring Boot application?
You need to put the Http method on your method, here I am assuming you are doing a GET request
#RestController
#RequestMapping("/check")
public class Controller {
#GetMapping // you forgot to put http method here
public String index(){
return "sdfksdjfkjkUshshdfisdfsdkasjdfjkasjdfkjakl:";
}
Note: GetMapping is only available if you are using Spring 4.3 or above else use #RequestMapping(value = "/url", method = RequestMethod.GET)
Your controller should be like this:
#RestController
public class Controller {
#RequestMapping(value="/check")
public String index(){
return "sdfksdjfkjkUshshdfisdfsdkasjdfjkasjdfkjakl:";
}
}
It seems
#RequestMapping(value="/check") is not working.
switch to
#RequestMapping(path="/check")
though as per documentation it should work.

How to get Spring REST to lead all endpoints with /v1/ after the domain and port?

How do I make a Spring REST service assign a value after the domain and port, but before the mapped endpoint? For example: http://{domain}/v1/...?
Example:
#RestController
#RequestMapping("home")
public class HomeController {
#RequestMapping("number")
public ResponseEntity getNumber() {
return ResponseEntity.ok(1);
}
}
curl http://localhost:8080/v1/home/number ~ 1
Also, is there a name for what I'm trying to achieve?
Assuming you're using Spring Data Rest, you can configure it in the RepositoryRestConfiguration #Configuration class like so:
public class SDRConfig extends RepositoryRestMvcConfiguration {
#Override
public RepositoryRestConfiguration config() {
RepositoryRestConfiguration config = super.config();
config.setBasePath("/v1");
return config;
}
}
Alternatively, with Spring Boot I believe it's
spring.data.rest.baseUri=api

Spring, how do I set the file shown when / is hit?

With Spring MVC 3, when a user goes to http://localhost/myspringapp/ how do I decide what page they will see? I currently get a 404.
You can set a mapping on a Controller:
#RequestMapping("/") #Controller
public class HomeController {
#RequestMapping
public String index() { /* your action */ }
You can also set a JSP file as welcome-file in your web.xml.

Resources