0

I am just Rest API concept with hello world program. I'm following some tutorial videos and trying the same program But I'm not getting the expected result.

Here is my Book.java

package com.book;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

@Path("/book")      //URI
public class Book {

    @GET
    @Produces(MediaType.TEXT_XML)
    public String sayHelloXML() {
        String response = "<?xml version='1.0'?>" + "<hello>Hello World</hello>";
        return response;
    }

}

and the web.xml

<?xml version="1.0" encoding="UTF-8"?>
<element>
    <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>WSdemo</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>JAVA WS</servlet-name>
      <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>

      <init-param>
            <param-name>jersey.config.server.provider.packages</param-name>
            <param-value>book</param-value>
      </init-param>

      <load-on-startup>1</load-on-startup>
      </servlet>

      <servlet-mapping>
            <servlet-name>JAVA WS</servlet-name>
            <url-pattern>/*</url-pattern>
      </servlet-mapping>

    </web-app>
</element>

And I'm using Tomcat v8.0 server. But as I run the application, I am getting out put as RestErrPic

Can somebody give me key ideas for learning the concept of RESTfull web service?

Shambhu
  • 121
  • 3
  • 11
  • Should be `com.book`, https://stackoverflow.com/questions/22994690/which-init-param-to-use-jersey-config-server-provider-packages-or-javax-ws-rs-a – PeterMmm Jun 25 '17 at 08:54

1 Answers1

0

There should be correct configuration in web.xml

I've edited my web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!-- <element> -->
    <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>WSdemo</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>JAVA WS</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.book</param-value>
      </init-param>

      <load-on-startup>1</load-on-startup>
      </servlet>

      <servlet-mapping>
            <servlet-name>JAVA WS</servlet-name>
            <url-pattern>/*</url-pattern>
      </servlet-mapping>

    </web-app>
<!-- </element> -->

And I got the correct output as output

Thank You.

Shambhu
  • 121
  • 3
  • 11