0

I'm trying to learn the ins and outs of using Eclipse to set up a web service. I've started with this tutorial. I was able to get something to work, but I'm finding that if I make simple changes to the Java code and try to rerun it, I can't get the changed version to run.

My class looks like this (imports suppressed):

package com.mypackage.mytest;

@Path("/helloworld")
public class HelloWorld {    
    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String sayPlainTextHello() {
        return "Hello World #1";
    }    
    @GET
    @Produces(MediaType.TEXT_XML)
    public String sayXMLHello() {
        return "<?xml version=\"1.0\"?>" + "<hello> Hello World #2 RESTful Jersey"
                + "</hello>";
    }    
    @GET
    @Produces(MediaType.TEXT_HTML)
    public String sayHtmlHello() {
        return "<html> " + "<title>" + "Hello World RESTful #3 Jersey"
                + "</title>" + "<body><h1>" + "Hello World #3 RESTful Jersey"
                + "</body></h1>" + "</html> ";
    }    
}

web.xml looks like:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
  <display-name>TestService</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
    <servlet-name>TestService</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
    <init-param>
        <param-name>jersey.config.server.provider.packages</param-name>
        <param-value>com.mypackage.mytest</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup> 
  </servlet>
  <servlet-mapping>
    <servlet-name>TestService</servlet-name>
    <url-pattern>/hello/*</url-pattern>
  </servlet-mapping>
</web-app>

I've set it up to use Tomcat 7.0, Servlet version 3.0. I'm running it by selecting "Run As -> Run On Server" on the project name.

If I bring up http://localhost:8080/TestService/hello/helloworld, it displays one of the above outputs.

However, if I change the string constants in the above Java code (e.g. changing #1 to #4, #2 to #5, #3 to #6), and attempt to rerun it, it still displays one of the old strings--the changes don't seem to take. I've tried different combinations of stopping the server, Build Project, and things that say "Publish", but so far the only things I've found that cause changes to appear are Project->Clean and restarting Eclipse. It doesn't make sense that I should have to clean my whole project to get changes to deploy, so I must be missing some other basic step ... but what?

ajb
  • 31,309
  • 3
  • 58
  • 84
  • 1
    Haven't tried., but maybe [this will help](http://stackoverflow.com/a/6189031/2587435) – Paul Samsotha Feb 20 '15 at 18:19
  • Thanks. I'm running Tomcat through Eclipse, and the linked question is from someone that is running the two independently. But it looks like it might point me in the right direction. – ajb Feb 21 '15 at 06:18
  • 1
    Also, if you are using Maven, see [tomcat-maven-plugin](http://tomcat.apache.org/maven-plugin-2.2/). I personally, don't use it, but from reading the documentation it appears to work similarly to the jetty-maven-plugin, which checks for updates every predetermined interval – Paul Samsotha Feb 21 '15 at 06:46
  • Thanks. I'm not currently using Maven, but I was planning on trying to try that later, so the info will be very helpful then. – ajb Feb 21 '15 at 07:08
  • Thanks again for the link--although it didn't really answer my question, by reading it and following some links in that answer, eventually I was able to figure things out. – ajb Feb 21 '15 at 07:40

1 Answers1

1

I'm now able to get things to work. Several things to note:

  1. I normally have Eclipse set up so that it builds automatically when I change a source file (Window->Preferences->General->Workspace). Somehow that setting got turned off. It could have been something that happened during the complex process of setting everything up (which involved a couple Install New Software's in Eclipse and some installation outside of it). Or it could have been something else I did wrong. Anyway, that would be one reason my changes didn't appear--I was assuming it was rebuilding automatically.

  2. The tutorial said to use "Run On Server" to deploy the new web app. I'm guessing this is necessary the first time. But after a change, "Run On Server" doesn't help at all; instead, I had to press the "Play" button on the Servers tab--and that was all that was necessary.

The server settings (specifically Publishing and Modules auto reload) didn't seem to make a difference.

ajb
  • 31,309
  • 3
  • 58
  • 84