• REST服务使用@RestController实例,输出xml/json


    REST服务使用@RestController实例,输出xml/json

    需要用到的服务注解

    org.springframework.web.bind.annotation.RestController
    

      

    用到的包:java和json互转

    jackson-mapper-asl #//java对象和json相互转化
    jackson-databind    #spring4.1+以上都必须包含(java对象和json相互转化)
    

      

    访问地址:

    http://localhost:8080/gugua9/hello/aaa.xml

    http://localhost:8080/gugua9/hello/aaa.json

    pom.xml

     <!-- 版本号 -->
      <properties>
      	<springVersion>4.3.5.RELEASE</springVersion>  
      </properties>
      
      <dependencies>
      
      
      
        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>3.8.1</version>
          <scope>test</scope>
        </dependency>
        
        
         <!-- spring-test支持 -->
        <dependency>
        	<groupId>org.springframework</groupId>
        	<artifactId>spring-test</artifactId>
        	<version>${springVersion}</version>
        </dependency>
        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>4.11</version>
          <scope>test</scope>
        </dependency>
        
          <!-- spring模块库 -->
        <dependency>
        	<groupId>org.springframework</groupId>
        	<artifactId>spring-beans</artifactId>
        	<version>${springVersion}</version>
        </dependency> 
        <dependency>
        	<groupId>org.springframework</groupId>
        	<artifactId>spring-web</artifactId>
        	<version>${springVersion}</version>
        </dependency>
        <dependency>
        	<groupId>org.springframework</groupId>
        	<artifactId>spring-webmvc</artifactId>
        	<version>${springVersion}</version>
        </dependency> 
        
        
        <!-- Servlet dependencies -->
        <!-- servlet(HttpServletRequest,HttpServletResponse) -->	
    		<dependency>
    			<groupId>javax.servlet</groupId>
    			<artifactId>javax.servlet-api</artifactId>
    			<version>3.1.0</version>
    		</dependency>
    		 <!-- servlet(HttpServletRequest,HttpServletResponse) -->	   
    		<dependency>
    		    <groupId>javax.servlet</groupId>
    		    <artifactId>jstl</artifactId>
    		    <version>1.2</version>
    		</dependency>
    		<dependency>
    			<groupId>javax.servlet.jsp</groupId>
    			<artifactId>javax.servlet.jsp-api</artifactId>
    			<version>2.3.1</version>
    		</dependency>
    		
    		<!-- https://mvnrepository.com/artifact/org.codehaus.jackson/jackson-mapper-asl -->
    		<!-- java对象和json相互转化 -->
    	<dependency>
    	    <groupId>org.codehaus.jackson</groupId>
    	    <artifactId>jackson-mapper-asl</artifactId>
    	    <version>1.9.13</version>
    	</dependency>
    	<dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.7.4</version>
        </dependency>
    		
    		
    		
      </dependencies>
      
      
      
      <build>
      	<pluginManagement>
      		<plugins>
      			<plugin>
      				<!-- 需要声明maven-plugin版本,用来管理包,否则会报错 -->
      				<groupId>org.apache.maven.plugins</groupId>
      				<artifactId>maven-war-plugin</artifactId>
      				<version>2.6</version>
      				<configuration>
      					<warSourceDirectory>src/main/webapp</warSourceDirectory>
      					<warName>gugua9</warName>
      					<!-- 屏蔽web.xml  -->
      					<failOnMissingWebXml>false</failOnMissingWebXml>
      				</configuration>
      			</plugin>
      			<!-- define the project compile level -->  
      			<plugin>
      				<groupId>org.apache.maven.plugins</groupId>
      				<artifactId>maven-compiler-plugin</artifactId>
      				<configuration>
      					<source>1.8</source>
      					<target>1.8</target>
      				</configuration>
      			</plugin>
      		</plugins>
      	</pluginManagement>
        <finalName>gugua9</finalName>
      </build>
    

      

    message.java

    加入xml注解就支持xml,不加入只支持json

    package springmvc.domain;
    
    import javax.xml.bind.annotation.XmlElement;
    import javax.xml.bind.annotation.XmlRootElement;
    
    //适应XML输出
    @XmlRootElement(name="message")
    public class Message {
    
    	String name;
    	String text;
    	
    	public Message(){
    		
    	}
    	
    	public Message(String name, String text) {
    		super();
    		this.name = name;
    		this.text = text;
    	}
    
    	@XmlElement
    	public String getName() {
    		return name;
    	}
    		
    	@XmlElement
    	public String getText() {
    		return text;
    	}
    	
    	
    }
    

      

    mvc/config 配置开启

    package springmvc.configuration;
    
    import org.springframework.context.annotation.ComponentScan;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.web.servlet.config.annotation.EnableWebMvc;
    
    //开启mvc,配置类
    @Configuration
    @EnableWebMvc
    @ComponentScan(basePackages="springmvc")
    public class HelloWorldConfiguration {
    
    	
    }
    

      

    初始化

    package springmvc.configuration;
    
    import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
    
    //初始化
    public class HelloWorldInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
    
    	@Override
    	protected Class<?>[] getRootConfigClasses() {
    		// TODO Auto-generated method stub
    		return new Class[] { HelloWorldConfiguration.class };
    	}
    
    	@Override
    	protected Class<?>[] getServletConfigClasses() {
    		// TODO Auto-generated method stub
    		return null;
    	}
    
    	@Override
    	protected String[] getServletMappings() {
    		// TODO Auto-generated method stub
    		return new String[]{"/"};
    	}
    
    }
    

      

    控制器:

    package springmvc.controller;
    
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RestController;
    
    import springmvc.domain.Message;
    
    @RestController
    public class HelloWorldRestController {
    
    	
    	@RequestMapping(value="/hello/{player}")
    	public Message message(@PathVariable String player  )
    	{
    		Message msg = new Message(player, "hello," + player);
    		
    		return msg;
    		
    	}
    	
    }
    

      

  • 相关阅读:
    第四章
    第三章随手笔记
    Android深度探索(卷1)HAL与驱动开发
    第十章心得体会
    第九章心得体会
    第八章心得体会
    第六章心得体会
    第七章心得体会
    第五章心得体会
    第四章心得体会
  • 原文地址:https://www.cnblogs.com/achengmu/p/9141167.html
Copyright © 2020-2023  润新知