1-8 SpringMVC常用视图Json序列化反序列化、Xml

1、视图解析的实现基础

ViewResolver 与 View 接口

    AbstractCachingViewResolver

    UrlBaseViewResolver

    FreeMarkerViewResolver

    ContentNegotiatingViewResolver

    InternalResourceViewResolver

 

DispatcherServlet中的视图解析逻辑

initStrategies()

    initViewResolvers()初始化了对应ViewResolver

doDispatch()

    processDispatchResult()

    返回没有视图的话 尝试RequestToViewNameTranslator

    resolveViewName()解析View对象

 

使用@ResponseBody的情况

在HandlerAdapter.handle()的中完成了Response输出

    RequestMappingHandlerAdapter.invokeHandlerMethod()

        HandlerMethodReturnValueHandlerComposite.handlerReturnValue()

            RequestResponseBodyMethodProcessor.handleReturnValue()

                genericConverter.write(body,... ...)

 

两种不同的重定向前缀

redirect 会丢失之前的信息

forward

 

2、常用视图 Json、Xml

序列化与反序列化:

extends StdDeserializer  \  extends StdSerializer

MoneyDeserializable

package com.example.ribi.support;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
import org.joda.money.CurrencyUnit;
import org.joda.money.Money;
import org.springframework.boot.jackson.JsonComponent;

import java.io.IOException;

@JsonComponent
public class MoneyDeserializable extends StdDeserializer {

    protected MoneyDeserializable() {
        super(Money.class);
    }

    @Override
    public Money deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
        return Money.of(CurrencyUnit.of("CNY"),p.getDecimalValue());
    }
}

MoneySerializable

package com.example.ribi.support;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
import org.joda.money.Money;
import org.springframework.boot.jackson.JsonComponent;

import java.io.IOException;

@JsonComponent
public class MoneySerializable extends StdSerializer {
    protected MoneySerializable() {
        super(Money.class);
    }

    @Override
    public void serialize(Money value, JsonGenerator gen, SerializerProvider provider) throws IOException {
        gen.writeNumber(value.getAmount());
    }
}

Controller

    @PostMapping(path = "/addJsonCoffee",consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Coffee addJsonCoffeeWithoutBindingResult(@RequestBody Coffee coffee){
        return coffee;
    }

Pom 

        
            com.fasterxml.jackson.dataformat
            jackson-dataformat-xml
            2.9.0
        
        
            com.fasterxml.jackson.datatype
            jackson-datatype-hibernate5
            2.9.8
        

 Main


    //支持Json缩进
    @Bean
    public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer(){
	    return builder -> builder.indentOutput(true);
    }

    //支持HibernateModule
    @Bean
    public Hibernate5Module hibernate5Module(){
	    return new Hibernate5Module();
    }

Test

localhost:8080/user/addJsonCoffee

Content-Type -> application/json;charset=utf-8
Accept -> application/json

{
    "name": "moca",
    "money": 12
}

localhost:8080/user/addJsonCoffee

Content-Type -> application/json;charset=utf-8
Accept -> application/xml


    moca
    12.00

 

你可能感兴趣的:(Springboot学习)