0

I am trying to make json webservice in spring .So I do the following steps

  • add the following library

Spring library

asm-3.3.1.jar
jersey-bundle-1.14.jar
spring-aop-4.1.3.RELEASE-javadoc.jar
spring-aop-4.1.3.RELEASE-sources.jar
spring-aop-4.1.3.RELEASE.jar
spring-aspects-4.1.3.RELEASE-javadoc.jar
spring-aspects-4.1.3.RELEASE-sources.jar
spring-aspects-4.1.3.RELEASE.jar
spring-beans-4.1.3.RELEASE-javadoc.jar
spring-beans-4.1.3.RELEASE-sources.jar
spring-beans-4.1.3.RELEASE.jar
spring-context-4.1.3.RELEASE-javadoc.jar
spring-context-4.1.3.RELEASE-sources.jar
spring-context-4.1.3.RELEASE.jar
spring-context-support-4.1.3.RELEASE-javadoc.jar
spring-context-support-4.1.3.RELEASE-sources.jar
spring-context-support-4.1.3.RELEASE.jar
spring-core-4.1.3.RELEASE-javadoc.jar
spring-core-4.1.3.RELEASE-sources.jar
spring-core-4.1.3.RELEASE.jar
spring-expression-4.1.3.RELEASE-javadoc.jar
spring-expression-4.1.3.RELEASE-sources.jar
spring-expression-4.1.3.RELEASE.jar
spring-instrument-4.1.3.RELEASE-javadoc.jar
spring-instrument-4.1.3.RELEASE-sources.jar
spring-instrument-4.1.3.RELEASE.jar
spring-instrument-tomcat-4.1.3.RELEASE-javadoc.jar
spring-instrument-tomcat-4.1.3.RELEASE-sources.jar
spring-instrument-tomcat-4.1.3.RELEASE.jar
spring-jdbc-4.1.3.RELEASE-javadoc.jar
spring-jdbc-4.1.3.RELEASE-sources.jar
spring-jdbc-4.1.3.RELEASE.jar
spring-jms-4.1.3.RELEASE-javadoc.jar
spring-jms-4.1.3.RELEASE-sources.jar
spring-jms-4.1.3.RELEASE.jar
spring-messaging-4.1.3.RELEASE-javadoc.jar
spring-messaging-4.1.3.RELEASE-sources.jar
spring-messaging-4.1.3.RELEASE.jar
spring-orm-4.1.3.RELEASE-javadoc.jar
spring-orm-4.1.3.RELEASE-sources.jar
spring-orm-4.1.3.RELEASE.jar
spring-oxm-4.1.3.RELEASE-javadoc.jar
spring-oxm-4.1.3.RELEASE-sources.jar
spring-oxm-4.1.3.RELEASE.jar
spring-test-4.1.3.RELEASE-javadoc.jar
spring-test-4.1.3.RELEASE-sources.jar
spring-test-4.1.3.RELEASE.jar
spring-tx-4.1.3.RELEASE-javadoc.jar
spring-tx-4.1.3.RELEASE-sources.jar
spring-tx-4.1.3.RELEASE.jar
spring-web-4.1.3.RELEASE-javadoc.jar
spring-web-4.1.3.RELEASE-sources.jar
spring-web-4.1.3.RELEASE.jar
spring-webmvc-4.1.3.RELEASE-javadoc.jar
spring-webmvc-4.1.3.RELEASE-sources.jar
spring-webmvc-4.1.3.RELEASE.jar
spring-webmvc-portlet-4.1.3.RELEASE-javadoc.jar
spring-webmvc-portlet-4.1.3.RELEASE-sources.jar
spring-webmvc-portlet-4.1.3.RELEASE.jar
spring-websocket-4.1.3.RELEASE-javadoc.jar
spring-websocket-4.1.3.RELEASE-sources.jar
spring-websocket-4.1.3.RELEASE.jar

create web.xml

<?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_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>Testspting</display-name>
  <display-name>Spring MVC Application</display-name>

   <servlet>
      <servlet-name>HelloWeb</servlet-name>
      <servlet-class>
         org.springframework.web.servlet.DispatcherServlet
      </servlet-class>
      <load-on-startup>1</load-on-startup>
   </servlet>

   <servlet-mapping>
      <servlet-name>HelloWeb</servlet-name>
      <url-pattern>/</url-pattern>
   </servlet-mapping>
</web-app>

create HelloWeb-servlet.xml

 <beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       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-3.0.xsd
       http://www.springframework.org/schema/context 
       http://www.springframework.org/schema/context/spring-context-3.0.xsd">

       <context:component-scan base-package="com.tutorialspoint" />

       <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
          <property name="prefix" value="/WEB-INF/jsp/" />
          <property name="suffix" value=".jsp" />
       </bean>

    </beans>

controller.js

@Controller
public class HelloController{

   @RequestMapping(value = "/hel", method = RequestMethod.GET ,consumes = "application/json" )
   public HashMap printHello(ModelMap model) {
       HashMap<String, String> map = new HashMap<String, String>();
       map.put("message", "Hello Spring MVC !");
      return map;
   }

   @GET
   @Path("/json/employees/")
   @Produces("application/json")
   public HashMap listEmployeesJSON(){
       HashMap<String, String> map = new HashMap<String, String>();
       map.put("message", "Hello Spring MVC !");
       return map;
   }

}

when I run my project like that http://localhost:8080/Testspting/hel it gives "Hello Spring MVC !" .it is fine

But when run like that http://localhost:8080/Testspting/json/employees server log say No mapping found for HTTP request with URI [/Testspting/json/employees] in DispatcherServlet with name 'HelloWeb'

my expected output will be when i run http://localhost:8080/Testspting/json/employees {"message", "Hello Spring MVC !"}

user944513
  • 12,247
  • 49
  • 168
  • 318
  • 1
    Shouldn't you try `http://localhost:8080/Testspting/json/employees/` ? – AKS Mar 16 '15 at 05:52
  • @AKS i tried but same result No mapping found for HTTP request with URI [/Testspting/json/employees/] in DispatcherServlet with name 'HelloWeb' – user944513 Mar 16 '15 at 05:59
  • Can you change `@Path("/json/employees/")` to `@Path("/json/employees")` and try it again. – Naman Gala Mar 16 '15 at 06:01
  • In that case, have a look here http://stackoverflow.com/questions/25543692/spring-restfull-jax-rs-annotation-support. You are using two different type of annotations - one using `@RequestMapping` and another using `@Path`. The answers to the given link explain in much detail. – AKS Mar 16 '15 at 06:02
  • @NamanGala not working ..is there any other way to make json – user944513 Mar 16 '15 at 06:05
  • @AKS is there any other way or other jar to make json – user944513 Mar 16 '15 at 06:05
  • Have a look at this example http://www.mkyong.com/spring-mvc/spring-3-mvc-and-json-example/ – Naman Gala Mar 16 '15 at 06:06
  • @user94451 I also don't understand why you would opt for two different type of annotations. Why not just go for the same annotation as for the first method? i.e. something like `@RequestMapping(value = "/json/employees", method = RequestMethod.GET ,produces = "application/json" )`. And, also use `@ResponseBody` as suggested by @Naman Gala. – AKS Mar 16 '15 at 06:08

2 Answers2

0

You can use @ResponseBody annotation along with @RequestMapping annotation.

Have a look at this example. http://www.mkyong.com/spring-mvc/spring-3-mvc-and-json-example/

For example

@RequestMapping(value = "/json/employees/{pathVariableIfAny}", method = { RequestMethod.GET })
public @ResponseBody
HashMap listEmployeesJSON(@PathVariable("pathVariableIfAny") int pathVariable) {
    HashMap<String, String> map = new HashMap<String, String>();
    map.put("message", "Hello Spring MVC !");
    return map;
}
Naman Gala
  • 4,670
  • 1
  • 21
  • 55
0
 @Controller
 @RequestMapping(value = "/Testspting")
 public class HelloController{
 @GET
 @Path("/json/employees/")
 @Produces("application/json")
 public HashMap listEmployeesJSON(){
           HashMap<String, String> map = new HashMap<String, String>();
           map.put("message", "Hello Spring MVC !");
           return map;
       }
  }

Try adding @RequestMapping annotation after @controller.

Alin
  • 314
  • 1
  • 3
  • 9
  • But as already mentioned, one of the links `http://localhost:8080/Testspting/hel` is working. So, this couldn't be the reason. – AKS Mar 16 '15 at 06:21
  • I think it will create problem for `/hel` url as it also include `@RequestMapping`. Have a look at [this](http://www.mkyong.com/spring-mvc/spring-mvc-multiactioncontroller-annotation-example/) example. – Naman Gala Mar 16 '15 at 06:22
  • 1
    I think you are combining Spring REST and Jersey JAX-RS. See this https://technoless.wordpress.com/2013/04/02/spring-rest-vs-jersey-jax-rs/ – Alin Mar 16 '15 at 06:27