当前位置: 首页 > wzjs >正文

正规网站设计制作公司网络推广的主要工作内容

正规网站设计制作公司,网络推广的主要工作内容,狗铺子做网页在那个网站,网站界面设计的原则在Spring Boot中,当使用RequestBody注解来接收HTTP请求中的JSON数据并将其转换为Java对象时,Spring默认会忽略额外的属性。这意味着如果发送的JSON包含一些目标对象中没有定义的属性,Spring不会报错,这些额外的属性会被简单地忽略…

在Spring Boot中,当使用@RequestBody注解来接收HTTP请求中的JSON数据并将其转换为Java对象时,Spring默认会忽略额外的属性。这意味着如果发送的JSON包含一些目标对象中没有定义的属性,Spring不会报错,这些额外的属性会被简单地忽略。

例如,假设有一个Java对象User,如下所示:

    private String name;private int age;// getters and setters
}

如果发送一个包含name和age以及额外属性email的JSON到Spring Boot应用,Spring会正常地将name和age映射到User对象,而email属性会被忽略:

{"name": "John","age": 30,"email": "john@example.com"
}

如果希望Spring在接收到包含未知属性的JSON时抛出错误,可以使用以下几种方法:

1. 使用@Validated和@JsonIgnoreProperties(ignoreUnknown = false)
可以通过自定义一个配置或者使用一个专门的类来处理JSON反序列化时的行为。例如,可以创建一个配置类来全局设置Jackson的属性忽略行为:

import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;@Configuration
public class JacksonConfig {@Beanpublic ObjectMapper jacksonObjectMapper(Jackson2ObjectMapperBuilder builder) {return builder.createXmlMapper(false).build().setConfig(builder.createXmlMapper(false).build().getDeserializationConfig().with(builder.deserializationConfig().with(builder.deserializationConfig().with(org.codehaus.jackson.map.DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, true))));}
}

2. 使用@JsonIgnoreProperties注解
在控制器方法参数上使用@JsonIgnoreProperties(ignoreUnknown = false)来强制要求所有未知属性都抛出异常:

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;@RestController
public class UserController {@PostMapping("/users")public User createUser(@RequestBody @JsonIgnoreProperties(ignoreUnknown = false) User user) {// 处理用户创建逻辑...return user;}
}

3. 使用自定义的转换器或反序列化器
创建一个自定义的反序列化器,并在其中显式地检查未知属性并抛出异常:

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import java.io.IOException;
import org.springframework.boot.jackson.JsonComponent;@JsonComponent
public class CustomUserDeserializer extends JsonDeserializer<User> {@Overridepublic User deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {JsonNode node = p.getCodec().readTree(p);if (!node.isObject()) {throw new IOException("Expected JSON object, but got " + node);} else {Iterator<Map.Entry<String, JsonNode>> fields = node.fields();User user = new User();while (fields.hasNext()) {Map.Entry<String, JsonNode> entry = fields.next();String fieldName = entry.getKey();if ("name".equals(fieldName) || "age".equals(fieldName)) { // 只允许这些字段存在,忽略其他所有字段。如果需要更复杂的行为,请根据需要调整此逻辑。// 设置user的属性...例如: user.setName(entry.getValue().asText()); 等等。 确保处理所有可能的字段。 否则,抛出异常或忽略。 例如: 抛出 new RuntimeException("Unknown property: " + fieldName); 如果想要抛出异常。 否则,可以选择忽略。 
http://www.dtcms.com/wzjs/358399.html

相关文章:

  • 网站建设 翻译网页设计与制作软件
  • 做网站网页排版错误云建站模板
  • 福建网站建设公司排名今天株洲最新消息
  • 做网站公司什么条件百度搜索链接入口
  • 莱特币做空 网站创建网址链接
  • 冠县做网站哪里好关键词排名提高
  • 北京建设网站的公司简介日本粉色iphone
  • 前端开发培训机构推荐无锡江东seo做关键词优化
  • 建设网站用英文怎么说百度竞价客服
  • 政府 网站 下载自媒体推广
  • 通江县政府网站四川建设网网站seo源码
  • 正邦设计招聘安卓系统优化软件
  • 太原集团网站建设谷歌商店下载
  • 温州模板网站建站河南网站建设哪个公司做得好
  • 上海广告牌制作公司临沂seo
  • 做网站 赚钱东莞网站开发公司
  • 微商怎么做网站搜索引擎推广的费用
  • 广州白云做网站网站怎么才能被百度收录
  • 租用网站如何制作网页产品推广思路
  • 网站备案密码格式百度网
  • 国内投资咨询网站 html模板西安网站建设公司
  • 都昌县建设局网站网络营销推广合作
  • 网站开发外贸网站的优化与推广分析
  • 网站公司怎么做的小红书怎么做关键词排名优化
  • 网站建设行情谷歌搜索引擎在线
  • 常州做网站的重庆百度推广开户
  • 公司做seo网站山西seo推广
  • 在线旅游网站建设方案网店如何营销推广
  • 怎么查询网站真假备案网站提交收录软件
  • 上海软件app开发流程深圳做seo有哪些公司