tomcat is not starting up when running app - java

this is pom xml
i have main class and one simple controller which returns string as json.
i am just running spring boot 2.x by adding below dependency.
in console it shows tomcat is started and application is started.but when i am hitting from postman or url in browser it will 404.
http://maven.apache.org/xsd/maven-4.0.0.xsd">
4.0.0
<groupId>com.kd</groupId>
<artifactId>restdemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>restdemo</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jersey</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
this is my controller:
package com.kd.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
#RestController
public class ServiceControllerKd {
#RequestMapping("/")
public Hello greeting() {
System.out.println("hello");
return new Hello();
}
}

I have taken your code, built it, ran it and hit the endpoint and have successfully received a response. However, it is difficult to assess what may be causing your issue without the full project.
I have created an example project using the code you provided here that runs successfully. It may be best to compare this project against your own to attempt to find any inconsistencies.
https://github.com/michaelmcfadyen/spring-boot-example

double check that you have your controller in the same package as your SpringBootApplication or in a child package
package com.kd;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
In the log you have to see something similar to
2018-07-28 12:34:01.669 INFO 10425 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/]}" onto java.lang.String com.kd.controller.ServiceControllerKd.greeting()

I think the problem is that you must first try to return a single string
example:
return "Hello world!"
To make sure that the problem is not from your Hello object.
Another thing that you have to make sure is that you have the right port on the postman request example http://localhost:8000/ and that your method on post man get the right verb, in your case you can add
#RequestMapping(value = "/", method = RequestMethod.GET)
and it's gonna be a "get".

Related

Getting 403 when one spring boot api is called from a java client although security dependencies are not added

I have created a simple spring boot application, with a get api which return "Hello world". The api works fine when called from postman or browser. However when I am trying to consume the api via JAX-RX(Jercy or RestEasy) or spring boots Rest Template in a different application, it gives 403 Forbidden (Authentication required) error.
Note: - My spring boot application have dependency only on spring-boot-starter-web and no other security dependency is added.
Following things I have tried but did not work
a. Adding dependency spring-boot-starter-security
b. Disabling csrf and cors.
c. authorizeRequests/authorizeHttpRequests anyRequests PermitAll.
Spring Boot Application
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>SpringServerApp</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>SpringServerApp</name>
<description>A simple spring rest server</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Controller class
package com.example.SpringServerApp.controller;
import org.springframework.web.bind.annotation.*;
#RestController
public class MainController {
#GetMapping("/helloGet")
String getHello() {
return "Hello World";
}
}
Client Code in a different application to call the api
package org.example.restClient;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.Response;
public class JercyClient {
private static final String GET_HELLO = "http://localhost:8080/helloGet";
private static final String SAMPLE_API = "https://catfact.ninja/fact";
static WebTarget webTarget;
static {
Client client = ClientBuilder.newClient();
webTarget = client.target(GET_HELLO);
}
public static String getHello() {
Response response = webTarget.request().get();
return response.readEntity(String.class);
}
}

spring boot application running but endpoint throwing 404

I was trying my hand at writing a sample Spring Boot application.
My initial thought was it's an easy example to follow and write but I am not able to print a simple statement upon hitting my endpoint.
I don't want to use any view (HTML,JSP etc) right now as I want to learn the backend first.
I have followed:
Getting Started
Spring Boot Guide
However, still stuck after a whole day of googling.
So let me reiterate the steps, I did.
Initialized a spring boot project from spring.io and downloaded zip.
Extracted the zip and imported it into Eclipse.
Configured a Rest Controller with an endpoint to just print a hello world statement.
Tomcat 9.0.56 is present on my system.
Running TodoApplication as Java application or Spring boot.
URL I am hitting is http://localhost:8080/greet
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
#RestController
public class HelloWorldController {
#GetMapping("/greet")
public String sayHelloWorld() {
return "Hello World";
}
}
Spring Boot Application:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
public class TodoApplication {
public static void main(String[] args) {
SpringApplication.run(TodoApplication.class, args);
}
}
pom file:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.sample</groupId>
<artifactId>todo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>todo</name>
<description>Back-end for To DO App</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
After navigating greet URL:
Got the following exception at console:
So the solution as pointed out by xerx593 is below:
Make sure to select packaging as war/jar in https://start.spring.io/.
Import the zip file.
Add your controller class in same or child package of SpringBootApplication, refer this for more info https://docs.spring.io/spring-boot/docs/current/reference/html/using.html#using.structuring-your-code.
Open terminal and change directory to working folder.
Run mvn spring-boot:run if using maven.
Hit the endpoint for example http://localhost:8080/greet or curl request.
Also I have noticed that 8080 port is listening but spring boot fails to register on it, in this case set
server.port = port other than 8080
in application.properties.

Spring Boot Controller 404

I am fairly new in Spring Boot and trying to build a simple web app. I have defined a controller class containing my mapping for url, but on browser it is giving me a white label page error(404). I am not able to understand why it is not able to map. I have tried changing my component scan base package, but it still doesn't redirect me to page "abc" or print "in controller" in the console.
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>bms</groupId>
<artifactId>com.wellmanage.bms</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>BMS</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Controller class
package com.wellmanage.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
#Controller
public class BookController {
#RequestMapping("/")
public String hello() {
System.out.println("in controller");
return "abc";
}
}
Main class
package com.wellmanage.bms;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
public class BmsApplication {
public static void main(String[] args) {
SpringApplication.run(BmsApplication.class, args);
}
}
The BmsApplication class is placed under the com.wellmanage.bms package and by default the #SpringBootApplication annotation runs component scan using this package as the root. Since the BookController is inside com.wellmanage.controller, the package is omitted by default configuration.
You have two options:
Change default component scan settings and set which package would you like to scan:
#ComponentScan("com.wellmanage")
#SpringBootApplication
public class BmsApplication {
public static void main(String[] args) {
SpringApplication.run(BmsApplication.class, args);
}
}
Move the main Application class to root package of you app so that all components which you want to scan automatically are under its package, e.g. to com.wellmanage.
From the configuration you have posted, it seems you didn't set any template engine. Returning the String "abc" from your controller is hence ignored.

404 error on main controller when using spring boot

I m trying to do Spring MVC tutorial:
It makes you create a main controller ad start a SpringApplication.
When I run the SpringApplication, I can see that a Tomcat server gets started, a controller class gets instanced as it should.
However, the mapping seems to fail : #RequestMapping("/greeting")
When I try to browse http://localhost:8080/TestSpringOpenEMM2/greeting
or http://localhost:8080/TestSpringOpenEMM2/greeting , I always get a 404 error.
(TestSpringOpenEMM2 is my project name).
I use eclipse IDE.
Here are my files:
package application;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
public class Application {
public static void main(String[] args) {
System.out.println("App starting" );
SpringApplication.run(Application.class, args);
System.out.println("App started" );
}
}
controller:
import javax.servlet.annotation.WebServlet;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
#Controller
public class GreetingController {
static{
System.out.println("Static init GreetingController");
}
#RequestMapping("/greeting")
public String greeting(#RequestParam(value="name", required=false, defaultValue="World") String name, Model model) {
System.out.println("starting servlet (greeting)");
model.addAttribute("name", name);
return "greeting";
}
public GreetingController(){
super();
System.out.println("new GreetingController");
}
}
pom:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>TestSpringOpenEMM2</groupId>
<artifactId>TestSpringOpenEMM2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<warSourceDirectory>WebContent</warSourceDirectory>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot</artifactId>
<version>1.2.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
<version>1.2.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.1.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-logging-juli</artifactId>
<version>8.0.23</version>
</dependency>
</dependencies>
</project>
Maybe you can use #RestController not #Controller. For REST api, if I use #Controller, the request will cause a 404 not found error, but after I change it to #RestController, the request works fine.
If you raise the Spring log level it should, when web-app starts, show you which URLs are being mapped to which classes/methods. That may help.
Also, if you're using Eclipse, right click project -> Properties -> Web Project Settings -> Context root. Make sure it is as you expect it to be i.e. TestSpringOpenEMM2. I have seen plenty of examples of this (context root) not being as expected in Eclipse.
The spring-boot-starter-parent is a great way to use Spring Boot, hence use a pom.xml like this:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>TestSpringOpenEMM2</groupId>
<artifactId>TestSpringOpenEMM2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.2.5.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
By default spring boot using a embedded tomcat container, you just need to run the main method. Read more on this topic on Spring Boot's Doc

Spring boot jersey error

I generated a project on start.spring.io with the following project dependencies:
Jersey (JAX-RS)
JPA
PostgreSQL
Web
When I try to access localhost:8080/homeroom/webapi/test I get a page with the following error:
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
I get this error no matter what url I try to access.
In my console info I can see it print Mapping servlet: 'jerseyServlet' to [/webapi/*] so I know my config class is being registered. When I change #ApplicationPath("/webapi") to #ApplicationPath("/"), a GET on localhost:8080/homeroom/test or localhost:8080/homeroom/ returns a blank page instead, with no text or error.
Why can't I access my resource?
This is my pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.homeroomed</groupId>
<artifactId>homeroom</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>HomeRoom</name>
<description>HomeRoom REST API</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.2.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jersey</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.4-1201-jdbc41</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
I'm trying to do a descriptor-less deploy so I have:
#Component
#ApplicationPath("/webapi")
public class MyJerseyConfig extends ResourceConfig{
public MyJerseyConfig() {
// String packageName =TestJerseyResource.class.getPackage().getName();
// this.packages(packageName);
this.register(TestJerseyResource.class);
// System.out.println("The package is:"+packageName);
}
}
And I have the following resource:
#Component
#Path("/test")
public class TestJerseyResource {
#GET
#Produces(MediaType.APPLICATION_JSON)
public String getTest()
{
return "Hi!";
}
}
I'm running the project from:
#SpringBootApplication
public class HomeRoomApplication {
public static void main(String[] args) {
SpringApplication.run(HomeRoomApplication.class, args);
}
}
UPDATE:
So 2 things:
I had to use spring annotations #Controller and #RequestMapping instead of Jersey's #PATH and #GET, and had to GET /webapi/test instead of /homeroom/webapi/test
The reason you can't access your resource from looking at the information you provided is you there is no /homeroom anywhere I saw in your code.
This is a valid URL for your project:
http://localhost:8080/webapi/test
If you wanted homeroom to be in the URL you could change the application path value to homeroom instead of webapi.
Well, quoting from #ApplicationPath JavaDoc:
May only be applied to a subclass of Application.
https://docs.oracle.com/javaee/6/api/javax/ws/rs/ApplicationPath.html
That might be part of the problem...

Categories

Resources