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

 

REST服务使用@RestController实例,输出xml/json_第1张图片

 

pom.xml

 
  
  	4.3.5.RELEASE  
  
  
  
  
  
  
    
      junit
      junit
      3.8.1
      test
    
    
    
     
    
    	org.springframework
    	spring-test
    	${springVersion}
    
    
      junit
      junit
      4.11
      test
    
    
      
    
    	org.springframework
    	spring-beans
    	${springVersion}
     
    
    	org.springframework
    	spring-web
    	${springVersion}
    
    
    	org.springframework
    	spring-webmvc
    	${springVersion}
     
    
    
    
    	
		
			javax.servlet
			javax.servlet-api
			3.1.0
		
		 	   
		
		    javax.servlet
		    jstl
		    1.2
		
		
			javax.servlet.jsp
			javax.servlet.jsp-api
			2.3.1
		
		
		
		
	
	    org.codehaus.jackson
	    jackson-mapper-asl
	    1.9.13
	
	
        com.fasterxml.jackson.core
        jackson-databind
        2.7.4
    
		
		
		
  
  
  
  
  
  	
  		
  			
  				
  				org.apache.maven.plugins
  				maven-war-plugin
  				2.6
  				
  					src/main/webapp
  					gugua9
  					
  					false
  				
  			
  			  
  			
  				org.apache.maven.plugins
  				maven-compiler-plugin
  				
  					1.8
  					1.8
  				
  			
  		
  	
    gugua9
  

  

 

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

  

 

你可能感兴趣的:(REST服务使用@RestController实例,输出xml/json)