Biscuit128 Biscuit128 - 3 months ago 8
Java Question

How to call rest end point

I have been working through a tutorial I found online - I am however having a couple of issues with being able to call my rest end point from a browser as I am unsure of the URL that needs to be used - I keep getting 404 errors.

I am not sure if i am missing some config or what not but my server seems to start fine in eclipse. Please would it be possible for someone to provide some help.

Thanks

Rest end point

@Path("/jersey-hello")
public class JerseyResource
{
private static final Logger LOGGER = Logger.getLogger(JerseyResource.class.getName());

@Autowired
private GreetingService greetingService;

@Inject
private DateTimeService timeService;

public JerseyResource()
{
LOGGER.info("HelloWorldResource()");
}

@GET
@Path("hi")
@Produces(MediaType.TEXT_PLAIN)
public String getHello()
{
return String.format("%s: %s", timeService.getDateTime(), greetingService.greet("world"));
}
}


Register resource configs

public class MyApplication extends ResourceConfig
{
public MyApplication()
{
register(RequestContextFilter.class);
register(JerseyResource.class);
register(SpringSingletonResource.class);
register(SpringRequestResource.class);
register(CustomExceptionMapper.class);
}
}


web.xml

<?xml version="1.0" encoding="UTF-8"?>

<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">

<module-name>restprj</module-name>

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:application-context.xml</param-value>
</context-param>

<servlet>
<servlet-name>SpringApplication</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>com.rest.test.demo.MyApplication</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>SpringApplication</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>

</web-app>


application-context

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

<bean id="greetingService"
class="com.rest.test.demo.GreetingServiceImpl" />

<bean class="com.rest.test.demo.DateTimeService"
scope="request" />

<bean
class="com.rest.test.demo.SpringSingletonResource" />

<bean
class="com.rest.test.demo.SpringRequestResource"
scope="request" />

<bean
class="com.rest.test.demo.CustomExceptionMapper" />
</beans>


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>
<version>1.0-SNAPSHOT</version>
<groupId>com.rest.test</groupId>

<artifactId>helloworld-spring-webapp</artifactId>
<packaging>war</packaging>
<name>jersey-examples-helloworld-spring-webapp</name>

<description>Spring 3 Integration Jersey Example</description>

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

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.3.2.RELEASE</version>
<scope>compile</scope>
</dependency>

<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1</version>
<exclusions>
<exclusion>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
</exclusion>
</exclusions>
</dependency>

<dependency>
<groupId>org.glassfish.jersey.ext</groupId>
<artifactId>jersey-spring3</artifactId>
<version>2.23.1</version>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<configuration>
<mainClass>org.glassfish.jersey.examples.helloworld.jaxrs.App</mainClass>
</configuration>
</plugin>
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>7.6.8.v20121106</version>
<configuration>
<webApp>
<descriptor>/src/main/resources/webapp/WEB-INF/web.xml</descriptor>
</webApp>
</configuration>
</plugin>
</plugins>
</build>

<profiles>
<profile>
<id>release</id>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>xml-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
</plugin>

</plugins>
</build>
</profile>
</profiles>

</project>


server startup

[INFO] Scanning for projects...
[WARNING]
[WARNING] Some problems were encountered while building the effective model for com.rest.test:helloworld-spring-webapp:war:1.0-SNAPSHOT
[WARNING] 'build.plugins.plugin.version' for org.codehaus.mojo:exec-maven-plugin is missing. @ line 66, column 12
[WARNING]
[WARNING] It is highly recommended to fix these problems because they threaten the stability of your build.
[WARNING]
[WARNING] For this reason, future Maven versions might no longer support building such malformed projects.
[WARNING]
Downloading: http://salnexus/nexus/content/groups/salerio/org/codehaus/mojo/exec-maven-plugin/maven-metadata.xml
Downloaded: http://salnexus/nexus/content/groups/salerio/org/codehaus/mojo/exec-maven-plugin/maven-metadata.xml (710 B at 3.8 KB/sec)
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building jersey-examples-helloworld-spring-webapp 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] >>> jetty-maven-plugin:7.6.8.v20121106:run (default-cli) @ helloworld-spring-webapp >>>
[INFO]
[INFO] --- maven-resources-plugin:2.5:resources (default-resources) @ helloworld-spring-webapp ---
[debug] execute contextualize
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 4 resources
[INFO]
[INFO] --- maven-compiler-plugin:3.5.1:compile (default-compile) @ helloworld-spring-webapp ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.5:testResources (default-testResources) @ helloworld-spring-webapp ---
[debug] execute contextualize
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory C:\dev-rl\hg\restprj\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.5.1:testCompile (default-testCompile) @ helloworld-spring-webapp ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] <<< jetty-maven-plugin:7.6.8.v20121106:run (default-cli) @ helloworld-spring-webapp <<<
[INFO]
[INFO] --- jetty-maven-plugin:7.6.8.v20121106:run (default-cli) @ helloworld-spring-webapp ---
[INFO] Configuring Jetty for project: jersey-examples-helloworld-spring-webapp
[INFO] webAppSourceDirectory not set. Defaulting to C:\dev-rl\hg\restprj\src\main\webapp
[INFO] Reload Mechanic: automatic
[INFO] Classes = C:\dev-rl\hg\restprj\target\classes
[INFO] Context path = /
[INFO] Tmp directory = C:\dev-rl\hg\restprj\target\tmp
[INFO] Web defaults = org/eclipse/jetty/webapp/webdefault.xml
[INFO] Web overrides = none
[INFO] web.xml file = /src/main/resources/webapp/WEB-INF/web.xml
[INFO] Webapp directory = C:\dev-rl\hg\restprj\src\main\webapp
2016-08-12 11:18:10.083:INFO:oejs.Server:jetty-7.6.8.v20121106
2016-08-12 11:18:10.603:INFO:oejpw.PlusConfiguration:No Transaction manager found - if your webapp requires one, please configure one.
2016-08-12 11:18:10.807:INFO:oejsh.ContextHandler:started o.m.j.p.JettyWebAppContext{/,file:/C:/dev-rl/hg/restprj/src/main/webapp/},file:/C:/dev-rl/hg/restprj/src/main/webapp/
2016-08-12 11:18:10.858:INFO:oejs.AbstractConnector:Started SelectChannelConnector@0.0.0.0:8080
[INFO] Started Jetty Server

Answer

I think it's easiest, if you move everything to the expected defaults folders.

webapp folder from src/main/resources/webapp to src/main/webapp.

I renamed application-context.xml to src/webapp/WEB-INF/applicationContext.xml and removed the context-param in web.xml.

I put a simple index.html with the content "hello" into src/webapp/index.html.

I removed the configuration tag from the jetty plugin in pom.xml.

After that I did mvn jetty:run. The I was able to http://localhost:8080/index.html.

Comments