Cannot get REST response (with Postman). Java, Int

2019-09-06 05:46发布

问题:

I'm only a beginner. I have this project that I was working on a training course. It was set up on Eclipse and it was working. I'm now trying to get this same code working using IntelliJ Ultimate and I cannot any rest response. I'm probably doing something wrong but I have no clue what it could be as I do not understand much when it comes setting things up. I tried both browser and Postman and I get error 404. I also tried to set a breakpoint to the code and it does not get activated. I created a new project, chose Web Aplication and RESTful web services from additional Libraries and Frameworks. I also added Maven framework. I have installed JDK 1.8 and Tomcat 9.

Below is my maven BOM. I'm not sure if all is correct:

<?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.wip.controller</groupId>
    <artifactId>TestApp</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>

        <dependency>
            <groupId>org.glassfish.jersey.core</groupId>
            <artifactId>jersey-server</artifactId>
            <version>2.25.1</version>
        </dependency>

        <dependency>
            <groupId>org.glassfish.jersey.containers</groupId>
            <artifactId>jersey-container-servlet</artifactId>
            <version>2.25.1</version>
        </dependency>


        <dependency>
            <groupId>com.sun.jersey</groupId>
            <artifactId>jersey-servlet</artifactId>
            <version>1.19.3</version>
        </dependency>

        <dependency>
            <groupId>com.sun.jersey</groupId>
            <artifactId>jersey-json</artifactId>
            <version>1.19.3</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>6.0.5</version>
        </dependency>

    </dependencies>
    
</project>

This is my web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">

    <display-name>Archetype Created Web Application</display-name>
    <servlet>
        <servlet-name>jersey-serlvet</servlet-name>
        <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
        <init-param>
            <param-name>com.sun.jersey.config.property.packages</param-name>
            <param-value>com.wip.controller</param-value>
        </init-param>
        <init-param>
            <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
            <param-value>true</param-value>
        </init-param>

        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>jersey-serlvet</servlet-name>
        <url-pattern>/rest/*</url-pattern>
    </servlet-mapping>
</web-app>

This is an example of one of my controller class.

package com.wip.controller;

import com.wip.bean.FoodGroup;
import com.wip.service.FoodGroupService;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import java.util.List;

@Path("/foodgroups")
public class FoodGroupController
{
	FoodGroupService foodGroupService = new FoodGroupService();

	@GET
	@Produces(MediaType.APPLICATION_JSON)
	public List<FoodGroup> getFoodGroups()
	{
		List<FoodGroup> listOfFoodGroups = foodGroupService.getAllFoodGroups();
		return listOfFoodGroups;
	}
}

I tried to use this on postman "localhost:8080/rest/foodgroups" This gives me a 404 as below:

<!DOCTYPE html>
<html>
    <head>
        <title>Apache Tomcat/9.0.0.M17 - Error report</title>
        <style type="text/css">h1 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22px;} h2 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:16px;} h3 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:14px;} body {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;} b {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;} p {font-family:Tahoma,Arial,sans-serif;background:white;color:black;font-size:12px;} a {color:black;} a.name {color:black;} .line {height:1px;background-color:#525D76;border:none;}</style>
    </head>
    <body>
        <h1>HTTP Status 404 - </h1>
        <div class="line"></div>
        <p>
            <b>type</b> Status report
        </p>
        <p>
            <b>message</b>
            <u></u>
        </p>
        <p>
            <b>description</b>
            <u>The requested resource is not available.</u>
        </p>
        <hr class="line">
        <h3>Apache Tomcat/9.0.0.M17</h3>
    </body>
</html>

There aren't any errors that come up with server log either.

D:\PROJECTS\Programming\apache-tomcat-9.0.0.M17\bin\catalina.bat run
[2017-03-26 07:51:13,166] Artifact TestApp:war exploded: Server is not connected. Deploy is not available.
Using CATALINA_BASE:   "C:\Users\laptop\.IntelliJIdea2016.3\system\tomcat\Tomcat_9_0_0_M17_TestApp"
Using CATALINA_HOME:   "D:\PROJECTS\Programming\apache-tomcat-9.0.0.M17"
Using CATALINA_TMPDIR: "D:\PROJECTS\Programming\apache-tomcat-9.0.0.M17\temp"
Using JRE_HOME:        "C:\Program Files\Java\jdk1.8.0_121"
Using CLASSPATH:       "D:\PROJECTS\Programming\apache-tomcat-9.0.0.M17\bin\bootstrap.jar;D:\PROJECTS\Programming\apache-tomcat-9.0.0.M17\bin\tomcat-juli.jar"
Connected to the target VM, address: '127.0.0.1:49794', transport: 'socket'
26-Mar-2017 19:51:13.971 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server version:        Apache Tomcat/9.0.0.M17
26-Mar-2017 19:51:13.972 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server built:          Jan 10 2017 20:59:20 UTC
26-Mar-2017 19:51:13.973 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server number:         9.0.0.0
26-Mar-2017 19:51:13.973 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log OS Name:               Windows 8.1
26-Mar-2017 19:51:13.973 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log OS Version:            6.3
26-Mar-2017 19:51:13.973 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Architecture:          amd64
26-Mar-2017 19:51:13.973 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Java Home:             C:\Program Files\Java\jdk1.8.0_121\jre
26-Mar-2017 19:51:13.973 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log JVM Version:           1.8.0_121-b13
26-Mar-2017 19:51:13.973 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log JVM Vendor:            Oracle Corporation
26-Mar-2017 19:51:13.973 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log CATALINA_BASE:         C:\Users\laptop\.IntelliJIdea2016.3\system\tomcat\Tomcat_9_0_0_M17_TestApp
26-Mar-2017 19:51:13.973 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log CATALINA_HOME:         D:\PROJECTS\Programming\apache-tomcat-9.0.0.M17
26-Mar-2017 19:51:13.974 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.util.logging.config.file=C:\Users\laptop\.IntelliJIdea2016.3\system\tomcat\Tomcat_9_0_0_M17_TestApp\conf\logging.properties
26-Mar-2017 19:51:13.974 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager
26-Mar-2017 19:51:13.974 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -agentlib:jdwp=transport=dt_socket,address=127.0.0.1:49794,suspend=y,server=n
26-Mar-2017 19:51:13.974 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcom.sun.management.jmxremote=
26-Mar-2017 19:51:13.974 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcom.sun.management.jmxremote.port=1099
26-Mar-2017 19:51:13.974 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcom.sun.management.jmxremote.ssl=false
26-Mar-2017 19:51:13.974 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcom.sun.management.jmxremote.authenticate=false
26-Mar-2017 19:51:13.974 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.rmi.server.hostname=127.0.0.1
26-Mar-2017 19:51:13.974 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djdk.tls.ephemeralDHKeySize=2048
26-Mar-2017 19:51:13.974 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.protocol.handler.pkgs=org.apache.catalina.webresources
26-Mar-2017 19:51:13.975 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcatalina.base=C:\Users\laptop\.IntelliJIdea2016.3\system\tomcat\Tomcat_9_0_0_M17_TestApp
26-Mar-2017 19:51:13.975 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcatalina.home=D:\PROJECTS\Programming\apache-tomcat-9.0.0.M17
26-Mar-2017 19:51:13.975 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.io.tmpdir=D:\PROJECTS\Programming\apache-tomcat-9.0.0.M17\temp
26-Mar-2017 19:51:13.975 INFO [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent Loaded APR based Apache Tomcat Native library 1.2.10 using APR version 1.5.2.
26-Mar-2017 19:51:13.975 INFO [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent APR capabilities: IPv6 [true], sendfile [true], accept filters [false], random [true].
26-Mar-2017 19:51:13.975 INFO [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent APR/OpenSSL configuration: useAprConnector [false], useOpenSSL [true]
26-Mar-2017 19:51:14.806 INFO [main] org.apache.catalina.core.AprLifecycleListener.initializeSSL OpenSSL successfully initialized (OpenSSL 1.0.2j  26 Sep 2016)
26-Mar-2017 19:51:14.908 INFO [main] org.apache.coyote.AbstractProtocol.init Initializing ProtocolHandler ["http-nio-8080"]
26-Mar-2017 19:51:14.922 INFO [main] org.apache.tomcat.util.net.NioSelectorPool.getSharedSelector Using a shared selector for servlet write/read
26-Mar-2017 19:51:14.924 INFO [main] org.apache.coyote.AbstractProtocol.init Initializing ProtocolHandler ["ajp-nio-8009"]
26-Mar-2017 19:51:14.926 INFO [main] org.apache.tomcat.util.net.NioSelectorPool.getSharedSelector Using a shared selector for servlet write/read
26-Mar-2017 19:51:14.926 INFO [main] org.apache.catalina.startup.Catalina.load Initialization processed in 1258 ms
26-Mar-2017 19:51:14.949 INFO [main] org.apache.catalina.core.StandardService.startInternal Starting service Catalina
26-Mar-2017 19:51:14.949 INFO [main] org.apache.catalina.core.StandardEngine.startInternal Starting Servlet Engine: Apache Tomcat/9.0.0.M17
26-Mar-2017 19:51:14.958 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler [http-nio-8080]
26-Mar-2017 19:51:14.965 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler [ajp-nio-8009]
26-Mar-2017 19:51:14.966 INFO [main] org.apache.catalina.startup.Catalina.start Server startup in 40 ms
Connected to server
[2017-03-26 07:51:15,313] Artifact TestApp:war exploded: Artifact is being deployed, please wait...
[2017-03-26 07:51:15,673] Artifact TestApp:war exploded: Artifact is deployed successfully
[2017-03-26 07:51:15,674] Artifact TestApp:war exploded: Deploy took 360 milliseconds
26-Mar-2017 19:51:24.965 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory D:\PROJECTS\Programming\apache-tomcat-9.0.0.M17\webapps\manager
26-Mar-2017 19:51:25.034 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory D:\PROJECTS\Programming\apache-tomcat-9.0.0.M17\webapps\manager has finished in 68 ms

I would be super grateful if someone has any ideas what to try next.


EDIT:

Thanks to the link provided by CrazyCoder I have manged to get my REST services to work. Like "localhost:8080/helloworld" works just fine and so some other links to some of my services work fine as well when I tested them through Postman. The only thing I'm struggling now is to get the index.html to come up.

I have tried pretty much every combination in my browser I can think of with no joy:

http://localhost:8080/
http://localhost:8080/index.html
http://localhost:8080/MyProject/index.html

And it keeps saying that This localhost page can’t be found

Latest screenshot of settings

If someone has an idea of what I could try, please advise. I'm just so close to get it running. Thank you.