Reputation: 1574
I am trying to make simple rest service which is used by everybody example it will consume by mobile developer.so I need to send static data to every one .I am trying to send static this data .
{
name:"abcd"
}
in other word if some one hit my system like this http://192.168.12.61:8080/springfirst/hello .then user get above json.
I follow this like to make http://www.programming-free.com/2014/03/spring-mvc-40-restful-web-service-json.html
I follow this step
here is my code web.xml
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Spring MVC Application</display-name>
<servlet>
<servlet-name>HelloWeb</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloWeb</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
hello-servelts.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
package com.tutorialspoint;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
@RequestMapping("/hello")
public class HelloController{
@RequestMapping( method = RequestMethod.GET,headers="Accept=application/json")
public String printHello(ModelMap model) {
return "abcd";
}
}
Upvotes: 0
Views: 820
Reputation: 3748
you have configuration problems:
So rename hello-servelts.xml
as HelloWeb-servlet.xml
.
and add @ResponseBody in your controller handler method to return JSON like:
@RequestMapping( method = RequestMethod.GET,headers="Accept=application/json")
public @ResponseBody Map printHello(ModelMap model) {
Map<String,String> json = new HashMap<String,String>();
json.put("name", "abcd");
return json;
}
Here is the working application using ContentNegotiatingViewResolver.
how to make rest service in spring mvc?
Ans, there is different ways are available. I am listing below some of:
@RequestBody
to read from HTTP request and @ResponseBody
to write a object as JSON into HTTP response.ContentNegotiatingViewResolver
where you can use it to resolve Views by request URL extension OR request ACCEPT header value. for example if URL is /view.html then it will return a view which has text/html
content-type. same you can configure it to return JSON as well.ContentNegotiatingViewResolver configuration for JSON View will look like:
<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<property name="order" value="1" />
<property name="mediaTypes">
<map>
<entry key="json" value="application/json" />
</map>
</property>
<property name="defaultViews">
<list>
<!-- JSON View -->
<bean class="org.springframework.web.servlet.view.json.MappingJackson2JsonView">
</bean>
</list>
</property>
<property name="ignoreAcceptHeader" value="true" />
</bean>
Note: Jackson mapper or any other mapper should be available on buildpath in order to work JSON serialize and deserialize.
If you use Maven, then confirm this dependency available in pom.xml:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.4.3</version>
</dependency>
SEE ALSO:
Upvotes: 2
Reputation: 6530
You can follow this guide which is an official documentation and it is using spring-boot
which will do easy your way to start writing services.
You rest service would be something like
@RestController
@RequestMapping("/hello")
public class HelloController{
@RequestMapping( method = RequestMethod.GET,headers="Accept=application/json")
@ResponseBody
public String printHello() {
return "abcd";
}
}
Upvotes: 0