如何在Spring MVC中进行休息服务?


问题内容

我正在尝试制作供每个示例使用的简单Rest服务,它将由移动开发人员使用。因此我需要向每个人发送静态数据。我正在尝试将这些数据静态发送。

{
name:"abcd"
}

换句话说,如果有人像我这样攻击我的系统
http://192.168.12.61:8080/springfirst/hello
,那么用户将获得高于json的权限。

我遵循这样来制作 http://www.programming-free.com/2014/03/spring-mvc-40-restful-web-
service-json.html

我按照这个步骤

  • 下载这些jar文件(-jackson-annotations-xxxjar-jackson-core-xxxjar-jackson-databind-xxxjar)并包含在lib文件夹中。

这是我的代码 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";
   }

}

问题答案:

您有配置问题:

  • 如果在没有上下文配置文件路径的情况下在web.xml中注册DispatcherServlet,则应根据 servletName-servlet.xml 命名上下文文件。

因此重命名hello-servelts.xmlHelloWeb-servlet.xml

并在控制器处理程序方法中添加@ResponseBody以返回JSON,例如:

 @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;
 }

是使用ContentNegotiatingViewResolver的工作应用程序。

如何在Spring MVC中进行休息服务?

回答,有不同的方法可用。我在下面列出了一些:

  • 要从HTTP请求或响应读取/写入JSON数据,应使用@RequestBody从HTTP请求读取和@ResponseBody将对象作为JSON写入HTTP响应的方法。
  • Spring提供了ContentNegotiatingViewResolver在哪里可以使用它来通过请求URL扩展或请求ACCEPT标头值解析视图。例如,如果URL为 /view.html, 则它将返回具有text/htmlcontent-type 的视图。同样,您也可以将其配置为返回JSON。

JSON视图的ContentNegotiatingViewResolver配置如下所示:

<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>

注意:在buildpath上应该可以使用Jackson映射器或任何其他映射器,以便进行JSON序列化和反序列化。

如果使用Maven,请确认pom.xml中可用的以下依赖关系:

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.4.3</version>
</dependency>