实体类型
package com.lsl.http.restonspringwebmvc.pojo;
public class Person {
private int id;
private String name;
private String sex;
@Override
public String toString() {
return "Person{" +
"id=" + id +
", name='" + name + '\'' +
", sex='" + sex + '\'' +
'}';
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
}
扩展Mime类型,这里是自定义一个properties类型,继承抽象类AbstractHttpMessageConverter重写三个方法,其中readInternal是读取body里的具体逻辑,writeInternal是将对象返回给客户端指定的Mime类型实现的逻辑
package com.lsl.http.restonspringwebmvc.message;
import com.lsl.http.restonspringwebmvc.pojo.Person;
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 java.io.*;
import java.nio.charset.Charset;
import java.util.Properties;
public class PropertiesPersonHttpMessageConverter
extends AbstractHttpMessageConverter<Person> {
public PropertiesPersonHttpMessageConverter(){
super(MediaType.valueOf("application/properties+person"));
setDefaultCharset(Charset.forName("UTF-8"));
}
@Override
protected boolean supports(Class<?> clazz) {
return clazz.isAssignableFrom(Person.class);
}
@Override
protected Person readInternal(Class<? extends Person> clazz
, HttpInputMessage inputMessage) throws IOException
, HttpMessageNotReadableException {
InputStream body = inputMessage.getBody();
Properties properties = new Properties();
properties.load(new InputStreamReader(body,getDefaultCharset()));
Person person = new Person();
person.setId((int)Long.parseLong(properties.getProperty("person.id")));
person.setName(properties.getProperty("person.name"));
person.setSex(properties.getProperty("person.sex"));
return person;
}
@Override
protected void writeInternal(Person person
, HttpOutputMessage outputMessage)
throws IOException
, HttpMessageNotWritableException {
OutputStream body = outputMessage.getBody();
Properties properties = new Properties();
properties.setProperty("person.id",String.valueOf(person.getId()));
properties.setProperty("person.name",person.getName());
properties.setProperty("person.sex",person.getSex());
properties.store(new OutputStreamWriter(body,getDefaultCharset()),"use properties show");
}
}
将自定义个的mime类型添加到集合,交给spring管理
package com.lsl.http.restonspringwebmvc.config;
import com.lsl.http.restonspringwebmvc.message.PropertiesPersonHttpMessageConverter;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import java.util.List;
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
converters.add(new PropertiesPersonHttpMessageConverter());
}
}
controller实现properties和json类型相互转化
package com.lsl.http.restonspringwebmvc.Controller;
import com.lsl.http.restonspringwebmvc.pojo.Person;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class PersonController {
@RequestMapping(value = "propertiesToJson"
,consumes = "application/properties+person" //请求类型 Content-Type
,produces = MediaType.APPLICATION_JSON_UTF8_VALUE) // 返回类型 accept
public Person jsonToProperties(@RequestBody Person person) {
//请求内容是properties
//返回内容是json
System.out.println(person);
return person;
}
@RequestMapping(value = "jsonToProperties"
,consumes = MediaType.APPLICATION_JSON_UTF8_VALUE
,produces = "application/properties+person"
)
public Person propertiesToJson(@RequestBody Person person) {
//请求内容是json
//返回内容是properties
System.out.println(person);
return person;
}
}
使用postman设置body为properties类型,返回回来是json类型
postman设置Accpet接受类型为json,Content-Type请求类型为自定义的properties类型
返回结果为json
——————————————————————————————————————
请求体为json
请求头如图
返回结果如图