1. 程式人生 > >springboot accep定義 (接受+返回) 的引數型別

springboot accep定義 (接受+返回) 的引數型別

1.返回的引數json轉xml 

1.匯入包

<dependency>
    <groupId>com.fasterxml.jackson.dataformat</groupId>
    <artifactId>jackson-dataformat-xml</artifactId>
    <version>2.9.6</version>
</dependency>

2.請求時加上請求頭 Accept application/xml即可

如果要返回json,去掉頭資訊,或者請求頭改為Accept application/json

3.原理

4.修改預設的返回格式

2.自定義返回型別

1.資料物件

public class Person {
	private String id;
	
	private String name;
 
	public String getId() {
		return id;
	}
 
	public void setId(String id) {
		this.id = id;
	}
 
	public String getName() {
		return name;
	}
 
	public void setName(String name) {
		this.name = name;
	}
}

2.處理類

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.nio.charset.Charset;
import java.util.Properties;
 
import org.springframework.http.HttpInputMessage;
import org.springframework.http.HttpOutputMessage;
import org.springframework.http.MediaType;
import org.springframework.http.converter.AbstractHttpMessageConverter;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.http.converter.HttpMessageNotWritableException;
 
import com.nn.bean.Person;
 
public class PropertiesPeopleHttpMessageConverter extends 
		AbstractHttpMessageConverter<Person>{
	
	public PropertiesPeopleHttpMessageConverter() {
		super(MediaType.valueOf("application/properties+person"));
		setDefaultCharset(Charset.forName("UTF-8"));
	}
	
	@Override
	protected boolean supports(Class<?> clazz) {
		return clazz.isAssignableFrom(Person.class);
	}
	
	/* 將請求內容中Properties內容轉化為Person物件
	 * (non-Javadoc)
	 * @see org.springframework.http.converter.AbstractHttpMessageConverter#readInternal(java.lang.Class, org.springframework.http.HttpInputMessage)
	 */
	@Override
	protected Person readInternal(Class<? extends Person> clazz, HttpInputMessage inputMessage)
			throws IOException, HttpMessageNotReadableException {
		
				/*person.id=1
				  person.name=小馬哥
				*/
		InputStream inputStream=inputMessage.getBody();
		Properties properties=new Properties();
//		將請求內容轉化到Properties
		properties.load(new InputStreamReader(inputStream, getDefaultCharset()));
//		將Properties內容轉化到Person物件欄位中
		Person person=new Person();
		person.setId(properties.getProperty("person.id"));
		person.setName(properties.getProperty("person.name"));
		
		return person;
	}
 
	
 
	@Override
	protected void writeInternal(Person person, HttpOutputMessage outputMessage)
			throws IOException, HttpMessageNotWritableException {
		OutputStream outputStream = outputMessage.getBody();
		Properties properties=new Properties();
		properties.setProperty("person.id", person.getId());
		properties.setProperty("person.name", person.getName());
		
		properties.store(new OutputStreamWriter(outputStream,getDefaultCharset()), "Written by web server");
		
	}
 
}

3.加入自定義的訊息轉換器

import java.util.List;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
 
import com.nn.filter.LoginFilter;
 
@Configuration
public class SpringMvcConfig implements WebMvcConfigurer{
 
	@Autowired
	private LoginFilter loginFilter;
	
	@Override
	public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
		converters.add(new PropertiesPeopleHttpMessageConverter());
	}
	
	
//	@Override
//	public void addInterceptors(InterceptorRegistry registry) {
//		registry.addInterceptor(loginFilter).addPathPatterns("/**").excludePathPatterns("/login","/");
//	}
}

4.controller

/**json轉properties
	 * @param person
	 * @return
	 */
	@PostMapping(value="/person/json/to/properties",
			produces="application/properties+person" //響應型別
			)
	@ResponseBody
	public Person personJsonToProperties(@RequestBody Person person) {
		return person;
	}
	
	/**properties轉json
	 * @param person
	 * @return
	 */
	@PostMapping(value="/person/properties/to/json",
			consumes="application/properties+person",  //請求型別
			produces=MediaType.APPLICATION_JSON_UTF8_VALUE //響應型別
			)
	@ResponseBody
	public Person propertiesJsonToPerson(@RequestBody Person person) {
		return person;
	}

5.請求示例