Spring 引导补丁 MongoDB
Spring Boot PATCH MongoDB
{
"id": "5e6a5f98003bb209b536a1be",
"firstName": "Alice",
"lastName": "Smith"
}
@Document
public class Customer {
@Id
public String id;
public String firstName;
public String lastName;
public Customer() {}
public Customer(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
@Override
public String toString() {
return String.format(
"Customer[id=%s, firstName='%s', lastName='%s']",
id, firstName, lastName);
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
如果我尝试仅更新下面的姓氏 JSON body- firstName 将设置为 NULL ..我如何避免这种情况?
那就是..只更新请求正文中的内容..不要将缺失的属性设置为空。
我正在使用 @PatchMapping("/customer/update")
{
"id": "5e6a5f98003bb209b536a1be",
"lastName": "Smith1"
}
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.core.MethodParameter;
import org.springframework.http.HttpInputMessage;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.http.server.ServletServerHttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.util.ReflectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;
import org.springframework.web.servlet.HandlerMapping;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
@Component
public class PartialUpdateArgumentResolver implements HandlerMethodArgumentResolver {
XmlMapper xmlMapper= new XmlMapper();
@Autowired ApplicationContext context;
@Override
public boolean supportsParameter(MethodParameter parameter) {
RequestMapping methodAnot = parameter.getMethodAnnotation(RequestMapping.class);
if( methodAnot == null ) return false;
if( !Arrays.asList(methodAnot.method()).contains(RequestMethod.PATCH) ) return false;
return parameter.hasParameterAnnotation(PatchRequestBody.class);
}
@Override
public Object resolveArgument(MethodParameter parameter,
ModelAndViewContainer mavContainer,
NativeWebRequest webRequest,
WebDataBinderFactory binderFactory) throws Exception {
ServletServerHttpRequest req = createInputMessage(webRequest);
Patch patch = parameter.getMethodAnnotation(Patch.class);
Class serviceClass = patch.service();
Class idClass = patch.id();
Object service = context.getBean(serviceClass);
String idStr = getPathVariables(webRequest).get("id");
Object id = idClass.cast(idStr);
Method method = ReflectionUtils.findMethod(serviceClass, "find", idClass);
Object obj = ReflectionUtils.invokeMethod(method, service, id);
obj = readJavaType(obj, req);
return obj;
}
private Map<String, String> getPathVariables(NativeWebRequest webRequest) {
HttpServletRequest httpServletRequest = webRequest.getNativeRequest(HttpServletRequest.class);
return (Map<String, String>) httpServletRequest.getAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE);
}
protected ServletServerHttpRequest createInputMessage(NativeWebRequest webRequest) {
HttpServletRequest servletRequest = webRequest.getNativeRequest(HttpServletRequest.class);
return new ServletServerHttpRequest(servletRequest);
}
private Object readJavaType(Object object, HttpInputMessage inputMessage) {
try {
return this.xmlMapper.readerForUpdating(object).readValue(inputMessage.getBody());
}
catch (IOException ex) {
throw new HttpMessageNotReadableException("Could not read document: " + ex.getMessage(), ex);
}
}
}
{
"id": "5e6a5f98003bb209b536a1be",
"firstName": "Alice",
"lastName": "Smith"
}
@Document
public class Customer {
@Id
public String id;
public String firstName;
public String lastName;
public Customer() {}
public Customer(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
@Override
public String toString() {
return String.format(
"Customer[id=%s, firstName='%s', lastName='%s']",
id, firstName, lastName);
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
如果我尝试仅更新下面的姓氏 JSON body- firstName 将设置为 NULL ..我如何避免这种情况? 那就是..只更新请求正文中的内容..不要将缺失的属性设置为空。
我正在使用 @PatchMapping("/customer/update")
{
"id": "5e6a5f98003bb209b536a1be",
"lastName": "Smith1"
}
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.core.MethodParameter;
import org.springframework.http.HttpInputMessage;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.http.server.ServletServerHttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.util.ReflectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;
import org.springframework.web.servlet.HandlerMapping;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
@Component
public class PartialUpdateArgumentResolver implements HandlerMethodArgumentResolver {
XmlMapper xmlMapper= new XmlMapper();
@Autowired ApplicationContext context;
@Override
public boolean supportsParameter(MethodParameter parameter) {
RequestMapping methodAnot = parameter.getMethodAnnotation(RequestMapping.class);
if( methodAnot == null ) return false;
if( !Arrays.asList(methodAnot.method()).contains(RequestMethod.PATCH) ) return false;
return parameter.hasParameterAnnotation(PatchRequestBody.class);
}
@Override
public Object resolveArgument(MethodParameter parameter,
ModelAndViewContainer mavContainer,
NativeWebRequest webRequest,
WebDataBinderFactory binderFactory) throws Exception {
ServletServerHttpRequest req = createInputMessage(webRequest);
Patch patch = parameter.getMethodAnnotation(Patch.class);
Class serviceClass = patch.service();
Class idClass = patch.id();
Object service = context.getBean(serviceClass);
String idStr = getPathVariables(webRequest).get("id");
Object id = idClass.cast(idStr);
Method method = ReflectionUtils.findMethod(serviceClass, "find", idClass);
Object obj = ReflectionUtils.invokeMethod(method, service, id);
obj = readJavaType(obj, req);
return obj;
}
private Map<String, String> getPathVariables(NativeWebRequest webRequest) {
HttpServletRequest httpServletRequest = webRequest.getNativeRequest(HttpServletRequest.class);
return (Map<String, String>) httpServletRequest.getAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE);
}
protected ServletServerHttpRequest createInputMessage(NativeWebRequest webRequest) {
HttpServletRequest servletRequest = webRequest.getNativeRequest(HttpServletRequest.class);
return new ServletServerHttpRequest(servletRequest);
}
private Object readJavaType(Object object, HttpInputMessage inputMessage) {
try {
return this.xmlMapper.readerForUpdating(object).readValue(inputMessage.getBody());
}
catch (IOException ex) {
throw new HttpMessageNotReadableException("Could not read document: " + ex.getMessage(), ex);
}
}
}