记一次解决“Required request part ‘file’ is not present”问题实录

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/dragonpeng2008/article/details/100077062

一、问题描述

数据库的主键采用的是17位的bigint,在实体类中使用Long类型存储,但是到前端会出现精度丢失的问题,于是就加了个配置类,在序列化的时候把Long类型转成字符串,解决了精度丢失的问题,但是却发现文件上传不了了。一直报“Required request part ‘file’ is not present”错误。

解决精度丢失的配置类代码如下

import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;


@EnableWebMvc
@Configuration
public class WebConfig	implements WebMvcConfigurer {

	/**
	 * 解决Long类型数值位数超过17位,前端解析时,精度丢失的bug
	 * @author changzhichen
	 * @date 2019-07-22 13:21
	 * @return org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer
	 **/
	@Bean("jackson2ObjectMapperBuilderCustomizer")
	public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer() {
		return jacksonObjectMapperBuilder -> jacksonObjectMapperBuilder.serializerByType(Long.class, ToStringSerializer.instance)
			.serializerByType(Long.TYPE, ToStringSerializer.instance);
	}
}

二、解决方案

去掉配置类上的@EnableWebMvc注解即可解决

原因可以参见这篇文章https://blog.csdn.net/zxc123e/article/details/84636521

通过查看@EnableWebMvc的源码,可以发现该注解就是为了引入一个DelegatingWebMvcConfiguration 配置类,而DelegatingWebMvcConfiguration又继承于WebMvcConfigurationSupport。也就是说,如果我们使用@EnableWebMvc就相当于导入了WebMvcConfigurationSupport类,这个时候,Spring Boot的自动装配就不会发生了,我们能用的,只有WebMvcConfigurationSupport提供的若干个配置。其实不使用@EnableWebMvc注解也是可以实现配置Webmvc,只需要将配置类继承于WebMvcConfigurationSupport类即可。

当使用@EnableWebMvc时,加载的是WebMvcConfigurationSupport中的配置项。

当不使用@EnableWebMvc时,使用的是WebMvcAutoConfiguration引入的配置项。

个人分析,应该是加了@EnableWebMvc导致Springboot自动配置失效,MultipartFile 参数不能被成功解析,导致文件上传失败。

 

文章创建于: 2019-08-26 14:32:13
展开阅读全文

DX11 HOOK present 问题

09-23

一个游戏是DX11的 我HOOK了它的present后 来绘制我自己想的图形,rn一般正常游玩下,它都有把图形绘制出来 但是如果组队后 游戏的界面会多出一个组队的队友信息 导致我的图形不被显示了rn但是如果在游戏内调出菜单栏或者 游戏内自己有文字提醒时,我绘制的图形又出现了.真的很奇怪 帮忙解答rnrn正常模式下rn[b][img=https://img-bbs.csdn.net/upload/201709/23/1506137272_905085.jpg][/img][/b]rnrn组队模式下,游戏左上角会多出一个组队框出来 这时候我的图形就不绘制了rn[img=https://img-bbs.csdn.net/upload/201709/23/1506137306_508213.png][/img]rnrn如果我呼出游戏的菜单或者游戏内有文字提醒输出在游戏画布上时,我画的东西又出现了rn[img=https://img-bbs.csdn.net/upload/201709/23/1506137348_611082.png][/img]rnrn绘制图形代码rnRenderer::Renderer(ID3D11Device *direct3DDevice, const std::wstring &defaultFontFamily) :rn direct3DDevice(direct3DDevice),rn immediateContext(nullptr),rn inputLayout(nullptr),rn vertexShader(nullptr),rn pixelShader(nullptr),rn fontFactory(nullptr),rn fontWrapper(nullptr),rn defaultFontFamily(defaultFontFamily),rn maxVertices(1024 * 4 * 3)rnrn D3D11_INPUT_ELEMENT_DESC layout[] =rn rn "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 ,rn "COLOR", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 16, D3D11_INPUT_PER_VERTEX_DATA, 0 ,rn ;rnrn ID3DBlob *vsBlob = nullptr;rn ID3DBlob *psBlob = nullptr;rnrn direct3DDevice->GetImmediateContext(&immediateContext);rnrn throwIfFailed(FW1CreateFactory(FW1_VERSION, &fontFactory));rnrn renderList = std::make_unique(fontFactory, maxVertices);rnrn throwIfFailed(fontFactory->CreateFontWrapper(direct3DDevice, defaultFontFamily.c_str(), &fontWrapper));rnrnrn HMODULE hD3DCompiler = LoadLibraryA("D3DCompiler_43.dll");rn if (hD3DCompiler == NULL) rn ExitProcess(0);rn rnrn m_pfnD3DCompile = reinterpret_cast(GetProcAddress(hD3DCompiler, "D3DCompile"));rn if (m_pfnD3DCompile == NULL) rn ExitProcess(0);rn rnrn throwIfFailed(m_pfnD3DCompile(shader, std::size(shader), nullptr, nullptr, nullptr, "VS", "vs_4_0", 0, 0, &vsBlob, nullptr));rn throwIfFailed(m_pfnD3DCompile(shader, std::size(shader), nullptr, nullptr, nullptr, "PS", "ps_4_0", 0, 0, &psBlob, nullptr));rnrn throwIfFailed(direct3DDevice->CreateVertexShader(vsBlob->GetBufferPointer(), vsBlob->GetBufferSize(), nullptr, &vertexShader));rn throwIfFailed(direct3DDevice->CreatePixelShader(psBlob->GetBufferPointer(), psBlob->GetBufferSize(), nullptr, &pixelShader));rnrn throwIfFailed(direct3DDevice->CreateInputLayout(layout, static_cast(std::size(layout)), vsBlob->GetBufferPointer(), vsBlob->GetBufferSize(), &inputLayout));rnrn safeRelease(vsBlob);rn safeRelease(psBlob);rnrnrn D3D11_BLEND_DESC blendDesc;rnrn blendDesc.RenderTarget->BlendEnable = TRUE;rn blendDesc.RenderTarget->SrcBlend = D3D11_BLEND_SRC_ALPHA;rn blendDesc.RenderTarget->DestBlend = D3D11_BLEND_INV_SRC_ALPHA;rn blendDesc.RenderTarget->SrcBlendAlpha = D3D11_BLEND_ONE;rn blendDesc.RenderTarget->DestBlendAlpha = D3D11_BLEND_ZERO;rn blendDesc.RenderTarget->BlendOp = D3D11_BLEND_OP_ADD;rn blendDesc.RenderTarget->BlendOpAlpha = D3D11_BLEND_OP_ADD;rn blendDesc.RenderTarget->RenderTargetWriteMask = D3D11_COLOR_WRITE_ENABLE_ALL;rnrn throwIfFailed(direct3DDevice->CreateBlendState(&blendDesc, &blendState));rnrn D3D11_BUFFER_DESC bufferDesc;rnrn bufferDesc.Usage = D3D11_USAGE_DYNAMIC;rn bufferDesc.ByteWidth = sizeof(Vertex) * static_cast(maxVertices);rn bufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;rn bufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;rn bufferDesc.MiscFlags = 0;rnrn throwIfFailed(direct3DDevice->CreateBuffer(&bufferDesc, nullptr, &vertexBuffer));rnrn bufferDesc = ;rnrn bufferDesc.Usage = D3D11_USAGE_DYNAMIC;rn bufferDesc.ByteWidth = sizeof(XMMATRIX);rn bufferDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;rn bufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;rn bufferDesc.MiscFlags = 0;rnrn throwIfFailed(direct3DDevice->CreateBuffer(&bufferDesc, nullptr, &screenProjectionBuffer));rnrn D3D11_VIEWPORT viewport;rn UINT numViewports = 1;rnrn immediateContext->RSGetViewports(&numViewports, &viewport);rnrn //DbgPrintA("%f, %f, %f, %f, %f, %f", viewport.TopLeftX, viewport.Width, viewport.Height, viewport.TopLeftY, viewport.MinDepth, viewport.MaxDepth);rnrn projection = XMMatrixOrthographicOffCenterLH(viewport.TopLeftX, viewport.Width, viewport.Height, viewport.TopLeftY, viewport.MinDepth, viewport.MaxDepth);rnrn D3D11_MAPPED_SUBRESOURCE mappedResource;rn throwIfFailed(immediateContext->Map(screenProjectionBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource));rn rn std::memcpy(mappedResource.pData, &projection, sizeof(XMMATRIX));rn rn immediateContext->Unmap(screenProjectionBuffer, 0);rnrnrnrnrnrn ID3D11Texture2D* RenderTargetTexture;rn if (SUCCEEDED(g_pSwapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), (LPVOID*)&RenderTargetTexture)))rn rnrn direct3DDevice->CreateRenderTargetView(RenderTargetTexture, NULL, &pRenderTargetView);rn RenderTargetTexture->Release();rn rnrnrnrnRenderer::~Renderer()rnrn safeRelease(vertexShader);rn safeRelease(pixelShader);rn safeRelease(vertexBuffer);rn safeRelease(inputLayout);rn safeRelease(blendState);rn safeRelease(fontWrapper);rn safeRelease(fontFactory);rnrnrnvoid Renderer::begin()rnrn immediateContext->OMSetRenderTargets(1, &pRenderTargetView, NULL);rnrnrn UINT numViewports = 1;rn D3D11_VIEWPORT viewport;rn immediateContext->RSGetViewports(&numViewports, &viewport);rnrn projection = XMMatrixOrthographicOffCenterLH(viewport.TopLeftX, viewport.Width, viewport.Height, viewport.TopLeftY, viewport.MinDepth, viewport.MaxDepth);rnrn D3D11_MAPPED_SUBRESOURCE mappedResource;rn throwIfFailed(immediateContext->Map(screenProjectionBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource));rn rn std::memcpy(mappedResource.pData, &projection, sizeof(XMMATRIX));rn rn immediateContext->Unmap(screenProjectionBuffer, 0);rnrnrn immediateContext->VSSetShader(vertexShader, nullptr, 0);rn immediateContext->PSSetShader(pixelShader, nullptr, 0);rnrn immediateContext->OMSetBlendState(blendState, nullptr, 0xffffffff);rnrn immediateContext->VSSetConstantBuffers(0, 1, &screenProjectionBuffer);rnrn immediateContext->IASetInputLayout(inputLayout);rnrn UINT stride = sizeof(Vertex);rn UINT offset = 0;rn immediateContext->IASetVertexBuffers(0, 1, &vertexBuffer, &stride, &offset);rnrnrnvoid Renderer::end()rnrn //pRenderTargetView->Release();rn renderList->clear();rnrnrnvoid Renderer::draw(const RenderList::Ptr &renderList)rnrn if (std::size(renderList->vertices) > 0)rn rn D3D11_MAPPED_SUBRESOURCE mappedResource;rn throwIfFailed(immediateContext->Map(vertexBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource));rn rn std::memcpy(mappedResource.pData, renderList->vertices.data(), sizeof(Vertex) * std::size(renderList->vertices));rn rn immediateContext->Unmap(vertexBuffer, 0);rn rn rn std::size_t pos = 0;rnrn for (const auto &batch : renderList->batches)rn rn immediateContext->IASetPrimitiveTopology(batch.topology);rn immediateContext->Draw(static_cast(batch.count), static_cast(pos));rnrn pos += batch.count;rn rn rn fontWrapper->Flush(immediateContext);rn fontWrapper->DrawGeometry(immediateContext, renderList->textGeometry, nullptr, nullptr, FW1_RESTORESTATE);rnrnrnvoid Renderer::draw()rnrn draw(renderList);rnrnrnrnrnHOOK及画图初始化rnHRESULT __stdcall MyPresent(IDXGISwapChain* pSwapChain, UINT SyncInterval, UINT Flags)rnrn rn static bool bInit = false;rn if (!bInit)rn rn g_pSwapChain = pSwapChain;rn pSwapChain->GetDevice(__uuidof(g_pDevice), (void**)&g_pDevice);rn renderer = std::make_unique(g_pDevice);rn bInit = true;rn rn renderer->begin();rn renderer->drawOutlinedRectA(20, 20, 300, 300, GColor::Blue(), GColor::Red());rn renderer->draw();rn renderer->end();rn HRESULT hRet = OLD_Present(pSwapChain, SyncInterval, Flags);rn return hRet;rnrnrn 论坛

没有更多推荐了,返回首页