I'm new in kotlin spring boot,write simple rest web service:
#SpringBootApplication
open class SpringKotlinWsApplication{
#GetMapping("/getUser")
fun getUser()= User("sample","sample")
}
fun main(args: Array<String>) {
runApplication<SpringKotlinWsApplication>(*args)
}
now configure local tomcat server on my computer,and when run the project and try to access that with this url:
http://localhost:4040/getUser
get this error:
HTTP Status 404 – Not Found
Type Status Report Message Not found 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 solve that problem?
My TOMCAT configuration image here
You forgot to add #RestController annotation
#RestController
#SpringBootApplication
open class SpringKotlinWsApplication{
#GetMapping("/getUser")
fun getUser() = User("sample","sample")
...
Related
I am using springdoc-open api for swagger integration in my springboot project.
I have added below property in application.yml
spring:
mvc:
pathmatch:
matching-strategy: ant_path_mathcher
and added below dependency in build.gralde file
implementation "org.springdoc:springdoc-openapi-ui:1.6.9"
I am able to access /v3/api-docs , but /swagger-ui/index.html and /swagger-ui.html giving 404 Whitelabel Error Page
After googling so many thins, I found a solution to this issue. I had to add an additional configuration class that is OpenApiConfig.java to make it work.
#Configuration
#EnableWebMvc
#ComponentScan(basePackages = {"org.springdoc"})
#Import({org.springdoc.core.SpringDocConfiguration.class,
org.springdoc.webmvc.core.SpringDocWebMvcConfiguration.class,
org.springdoc.webmvc.ui.SwaggerConfig.class,
org.springdoc.core.SwaggerUiConfigProperties.class,
org.springdoc.core.SwaggerOAuthProperties.class,
org.springframework.autoconfigure.jackson.JacksonAutoConfiguration.class})
class OpenApiConfig implements WebMvcConfigurer {
}
I'm new postman, just installed it. I have spring boot simple app build with maven
and run in embedded tomcat through STS.
I'm try send get request:
http://localhost/9009/locations/Cntry
Here is the code from the controller:
#RestController
#RequestMapping("locations")
#Slf4j
public class LocationController {
#Autowired
LocationService locationService;
#GetMapping("Cntry")
public List<Country> getCountries() {
return locationService.getdCountries();
}
I'm getting 404 error from postman when clicking send:
{
"timestamp": "2021-07-09T03:40:36.764+00:00",
"status": 404,
"error": "Not Found",
"path": "/9009/locations/Cntry"
}
I have 401 error before and I fixed it by comment spring security from pom file (I didn't
implement security yet). Ideally the spring security should be uncomment.
Thanks.
Your postman URL wrong. And correct the URL at controller level.
#GetMapping("/Cntry")
#RequestMapping("/locations")
9009 port number that's should come as below.
And confirm your PORT number correct or not by default it will run at 8080 if you want customize running port run then Add below configuration in application properties file.
server.port=9009
http://localhost:9009/locations/Cntry
I am learning swagger using the example pet store in the swagger editor: https://editor.swagger.io/
The code for spring is generated and I does not change anything. However, everytime I run it on http://localhost:8080, it gives the error message
This localhost page can’t be found
No webpage was found for the web address: http://localhost:8080/
HTTP ERROR 404
But I believe I am supposed to see something like in the following website:
https://petstore.swagger.io/
May I ask how to solve this issue? Many thanks.
I follow the URL in the controller class HomeController.
#Controller
public class HomeController {
#RequestMapping(value = "/")
public String index() {
System.out.println("swagger-ui.html");
return "redirect:swagger-ui.html";
}
}
You might be visiting the wrong URL. Try out http://localhost:8080/<base-url>/swagger-ui.html
Additionally, you could refer this link for a basic setup.
It appears the context path is v2.
You should be able to access the demo at:
http://localhost:8080/v2/<endoint>
Try accessing:
http://localhost:8080/v2/swagger-ui.html
The context path is defined in a configuration file titled application.properties located under src/main/resource. The file contains the following:
springfox.documentation.swagger.v2.path=/api-docs
server.contextPath=/v2
server.port=8080
spring.jackson.date-format=io.swagger.RFC3339DateFormat
spring.jackson.serialization.WRITE_DATES_AS_TIMESTAMPS=false
The context path is defined under server.contextPath.
I'm attempting to make a REST call to a local Tomcat9 server using Spring. My controller is as follows.
#RestController
public class HelloWorldController {
#RequestMapping(method=RequestMethod.GET, path="/hello-world")
public String helloWorld() {
return "Hello World";
}
}
However, when I attempt the REST requesting using the url, http://localhost:8080/hello-world, I receive the following 404 error:
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 resolve this? I'm trying to gain more practice with Spring.
EDIT: I resolved the issue. I stopped the Tomcat server and ran RestfulWebServicesApplication as a Java application.
I often saw such kind of error logs on the running server:
ERROR o.s.boot.web.support.ErrorPageFilter - Cannot forward to error page for request [/api/login] as the response has already been committed. As a result, the response may have the wrong status code. If your application is running on WebSphere Application Server you may be able to resolve this problem by setting com.ibm.ws.webcontainer.invokeFlushAfterService to false
My server running & development environment:
Ubuntu 14.04
Tomcat 8
Sprint Framework: 4.3.1
Sprint Boot: 1.5.9
Kotlin: 1.2.21
According to one question in stackoverflow, the following code which I have added (transformed as Kotlin code) to try removing this error log but doesn't work.
#SpringBootApplication
#EnableScheduling
#EnableAsync
#EnableTransactionManagement
open class MyApplication: SpringBootServletInitializer() {
init {
setRegisterErrorPageFilter(false) <<<< I added this func call to try fix
}
companion object {
#JvmStatic fun main(args: Array<String>) {
SpringApplication.run(MyApplication::class.java, *args)
}
}
}
Could anybody give a further hint? about how to fix the error.