Spring:根据 XSD 模式验证 REST 控制器
Spring: Validate REST controller against XSD schema
目前我有带有以下代码的 RestController
package be.smartask.api;
import be.smartask.api.model.NumberValue;
import be.smartask.api.model.TextValue;
import be.smartask.api.model.Translations;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
/**
* @author Glenn Van Schil
* Created on 21/01/2016
*/
@CrossOrigin
@RestController
@RequestMapping(path = "/values")
public class Controller {
@RequestMapping(method = RequestMethod.POST, headers = "Value-Type=text")
ResponseEntity<?> createAttribute(@RequestBody TextValue value) {
System.out.println(value.getCode());
for (Translations.Translation translation : value.getTranslations().getTranslation()) {
System.out.println(translation.getLang());
System.out.println(translation.getValue());
}
return new ResponseEntity<>(HttpStatus.CREATED);
}
@RequestMapping(method = RequestMethod.POST, headers = "Value-Type=number")
ResponseEntity<?> createAttribute(@RequestBody NumberValue value) {
System.out.println(value.getMinValue());
System.out.println(value.getMaxValue());
return new ResponseEntity<>(HttpStatus.CREATED);
}
}
效果很好,我可以 post 像这样的 NumberValue:
<numberValue id="id" minValue="0" maxValue="10"/>
但是当我 post 一个 TextValue 到 NumberValue 方法时
<textvalue code="LUXE">
<translations>
<translation lang="en">luxury car</translation>
</translations>
</textvalue>
它仍然有效。它只是将 minValue 和 maxValue 保留为 0,0。
我的问题是:当正文与我的 xsd 文件中定义的不完全相同时,我如何强制执行 400:错误请求(或类似的请求)?根据定义,我的意思是 xml 标签的名称不相同或缺少必需的 xs:attribute,...
我的 xsd 文件:
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<!-- ValueVO -->
<xs:complexType name="geopoint">
<xs:attribute type="xs:double" name="lat"/>
<xs:attribute type="xs:double" name="lon"/>
</xs:complexType>
<xs:complexType name="translations">
<xs:sequence>
<xs:element name="translation" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute type="xs:string" name="lang" use="required"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
<xs:complexType name="value">
<xs:attribute type="xs:string" name="id" use="required"/>
</xs:complexType>
<xs:complexType name="textValue">
<xs:complexContent>
<xs:extension base="value">
<xs:sequence>
<xs:element type="translations" name="translations"/>
</xs:sequence>
<xs:attribute type="xs:string" name="code" use="required"/>
</xs:extension>
</xs:complexContent>
</xs:complexType>
<xs:complexType name="numberValue">
<xs:complexContent>
<xs:extension base="value">
<xs:attribute type="xs:double" name="minValue" use="required"/>
<xs:attribute type="xs:double" name="maxValue" use="required"/>
</xs:extension>
</xs:complexContent>
</xs:complexType>
<xs:complexType name="dateValue">
<xs:complexContent>
<xs:extension base="value">
<xs:attribute type="xs:date" name="minValue" use="required"/>
<xs:attribute type="xs:date" name="maxValue" use="required"/>
</xs:extension>
</xs:complexContent>
</xs:complexType>
<xs:complexType name="cityValue">
<xs:complexContent>
<xs:extension base="value">
<xs:sequence>
<xs:element type="geopoint" name="geopoint"/>
<xs:element type="translations" name="translations"/>
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
<xs:complexType name="timeValue">
<xs:complexContent>
<xs:extension base="value"/>
</xs:complexContent>
</xs:complexType>
<xs:element name="value" type="value"/>
<xs:element name="textValue" type="textValue"/>
<xs:element name="numberValue" type="numberValue"/>
<xs:element name="dateValue" type="dateValue"/>
<xs:element name="cityValue" type="cityValue"/>
<xs:element name="timeValue" type="timeValue"/>
<xs:element name="values">
<xs:complexType>
<xs:sequence>
<xs:element ref="value" maxOccurs="unbounded" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<!-- END ValueVO -->
</xs:schema>
更新
我根据 Sheetal Mohan Sharma 的回答将以下内容添加到我的 spring 配置中
<?xml version="1.0" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="be.smartask.api"/>
<mvc:annotation-driven/>
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="marshallingHttpMessageConverter"/>
<bean class="org.springframework.http.converter.StringHttpMessageConverter"/>
</list>
</property>
</bean>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
</bean>
<bean id="marshallingHttpMessageConverter"
class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter"
p:marshaller-ref="jaxb2Marshaller" p:unmarshaller-ref="jaxb2Marshaller"/>
<bean id="jaxb2Marshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
<property name="schema" value="classpath:schema/xsd/smartaskRead.xsd"/>
<property name="classesToBeBound">
<list>
<value>be.smartask.api.model.NumberValue</value>
<value>be.smartask.api.model.TextValue</value>
</list>
</property>
</bean>
</beans>
但是 NumberValue 仍然接受 TextValue...
你可以看看使用 jaxb marshaller 和 unmarshaller
使用消息转换器和 jaxmashaller 检查 XSD。如果验证失败,将抛出错误,但它可能不是特定的。检查下面的链接。
<bean id="marshallingHttpMessageConverter"
class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter"p:marshaller-ref="jaxb2Marshaller" p:unmarshaller-ref="jaxb2Marshaller" />
<bean id="jaxb2Marshaller">
<property name="schema" value="classpath:/mySchema.xsd"/>
<property name="classesToBeBound">
<list>
<value>com.xyz.RequestPojo</value>
<value>com.xyz.ResponsePojo</value>
</list>
</property>
</bean>
即使使用 API 版本控制,我们也设法做到这一点,方法是在 XSD 文件中提供命名空间
config.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd">
<context:component-scan base-package="be.smartask.api"/>
<context:annotation-config/>
<mvc:cors>
<mvc:mapping path="/**"/>
</mvc:cors>
<mvc:annotation-driven>
<mvc:message-converters>
<bean id="marshallingHttpMessageConverter"
class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter">
<property name="marshaller" ref="jaxb2Marshaller"/>
<property name="unmarshaller" ref="jaxb2Marshaller"/>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
<bean id="jaxb2Marshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
<property name="schemas">
<list>
<value>classpath:/schema/xsd/v1/smartAsk.xsd</value>
<value>classpath:/schema/xsd/v2/smartAsk.xsd</value>
</list>
</property>
<property name="packagesToScan">
<list>
<value>be.smartask.api.model.smartask.v1</value>
<value>be.smartask.api.model.smartask.v2</value>
</list>
</property>
</bean>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
首先您提供模式位置,其次是将生成 xsd 对象的包位置,最后用您新创建的 Jaxb2Marshaller
覆盖 marshallingHttpMessageConverter
如果您计划在 API 中进行一些版本控制,最好提供一个命名空间,以便编组器知道哪个 xsd 文件用于哪个包
<xs:schema xmlns="smartask:v1"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
xmlns:annox="http://annox.dev.java.net"
attributeFormDefault="unqualified"
elementFormDefault="qualified"
targetNamespace="smartask:v1"
jaxb:version="2.1"
jaxb:extensionBindingPrefixes="annox">
<xs:schema xmlns="smartask:v2"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
xmlns:annox="http://annox.dev.java.net"
attributeFormDefault="unqualified"
elementFormDefault="qualified"
targetNamespace="smartask:v2"
jaxb:version="2.1"
jaxb:extensionBindingPrefixes="annox">
在我们的例子中是 "smartask:v1" 和 "smartask:v2"
目前我有带有以下代码的 RestController
package be.smartask.api;
import be.smartask.api.model.NumberValue;
import be.smartask.api.model.TextValue;
import be.smartask.api.model.Translations;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
/**
* @author Glenn Van Schil
* Created on 21/01/2016
*/
@CrossOrigin
@RestController
@RequestMapping(path = "/values")
public class Controller {
@RequestMapping(method = RequestMethod.POST, headers = "Value-Type=text")
ResponseEntity<?> createAttribute(@RequestBody TextValue value) {
System.out.println(value.getCode());
for (Translations.Translation translation : value.getTranslations().getTranslation()) {
System.out.println(translation.getLang());
System.out.println(translation.getValue());
}
return new ResponseEntity<>(HttpStatus.CREATED);
}
@RequestMapping(method = RequestMethod.POST, headers = "Value-Type=number")
ResponseEntity<?> createAttribute(@RequestBody NumberValue value) {
System.out.println(value.getMinValue());
System.out.println(value.getMaxValue());
return new ResponseEntity<>(HttpStatus.CREATED);
}
}
效果很好,我可以 post 像这样的 NumberValue:
<numberValue id="id" minValue="0" maxValue="10"/>
但是当我 post 一个 TextValue 到 NumberValue 方法时
<textvalue code="LUXE">
<translations>
<translation lang="en">luxury car</translation>
</translations>
</textvalue>
它仍然有效。它只是将 minValue 和 maxValue 保留为 0,0。
我的问题是:当正文与我的 xsd 文件中定义的不完全相同时,我如何强制执行 400:错误请求(或类似的请求)?根据定义,我的意思是 xml 标签的名称不相同或缺少必需的 xs:attribute,...
我的 xsd 文件:
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<!-- ValueVO -->
<xs:complexType name="geopoint">
<xs:attribute type="xs:double" name="lat"/>
<xs:attribute type="xs:double" name="lon"/>
</xs:complexType>
<xs:complexType name="translations">
<xs:sequence>
<xs:element name="translation" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute type="xs:string" name="lang" use="required"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
<xs:complexType name="value">
<xs:attribute type="xs:string" name="id" use="required"/>
</xs:complexType>
<xs:complexType name="textValue">
<xs:complexContent>
<xs:extension base="value">
<xs:sequence>
<xs:element type="translations" name="translations"/>
</xs:sequence>
<xs:attribute type="xs:string" name="code" use="required"/>
</xs:extension>
</xs:complexContent>
</xs:complexType>
<xs:complexType name="numberValue">
<xs:complexContent>
<xs:extension base="value">
<xs:attribute type="xs:double" name="minValue" use="required"/>
<xs:attribute type="xs:double" name="maxValue" use="required"/>
</xs:extension>
</xs:complexContent>
</xs:complexType>
<xs:complexType name="dateValue">
<xs:complexContent>
<xs:extension base="value">
<xs:attribute type="xs:date" name="minValue" use="required"/>
<xs:attribute type="xs:date" name="maxValue" use="required"/>
</xs:extension>
</xs:complexContent>
</xs:complexType>
<xs:complexType name="cityValue">
<xs:complexContent>
<xs:extension base="value">
<xs:sequence>
<xs:element type="geopoint" name="geopoint"/>
<xs:element type="translations" name="translations"/>
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
<xs:complexType name="timeValue">
<xs:complexContent>
<xs:extension base="value"/>
</xs:complexContent>
</xs:complexType>
<xs:element name="value" type="value"/>
<xs:element name="textValue" type="textValue"/>
<xs:element name="numberValue" type="numberValue"/>
<xs:element name="dateValue" type="dateValue"/>
<xs:element name="cityValue" type="cityValue"/>
<xs:element name="timeValue" type="timeValue"/>
<xs:element name="values">
<xs:complexType>
<xs:sequence>
<xs:element ref="value" maxOccurs="unbounded" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<!-- END ValueVO -->
</xs:schema>
更新
我根据 Sheetal Mohan Sharma 的回答将以下内容添加到我的 spring 配置中
<?xml version="1.0" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="be.smartask.api"/>
<mvc:annotation-driven/>
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="marshallingHttpMessageConverter"/>
<bean class="org.springframework.http.converter.StringHttpMessageConverter"/>
</list>
</property>
</bean>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
</bean>
<bean id="marshallingHttpMessageConverter"
class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter"
p:marshaller-ref="jaxb2Marshaller" p:unmarshaller-ref="jaxb2Marshaller"/>
<bean id="jaxb2Marshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
<property name="schema" value="classpath:schema/xsd/smartaskRead.xsd"/>
<property name="classesToBeBound">
<list>
<value>be.smartask.api.model.NumberValue</value>
<value>be.smartask.api.model.TextValue</value>
</list>
</property>
</bean>
</beans>
但是 NumberValue 仍然接受 TextValue...
你可以看看使用 jaxb marshaller 和 unmarshaller 使用消息转换器和 jaxmashaller 检查 XSD。如果验证失败,将抛出错误,但它可能不是特定的。检查下面的链接。
<bean id="marshallingHttpMessageConverter"
class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter"p:marshaller-ref="jaxb2Marshaller" p:unmarshaller-ref="jaxb2Marshaller" />
<bean id="jaxb2Marshaller">
<property name="schema" value="classpath:/mySchema.xsd"/>
<property name="classesToBeBound">
<list>
<value>com.xyz.RequestPojo</value>
<value>com.xyz.ResponsePojo</value>
</list>
</property>
</bean>
即使使用 API 版本控制,我们也设法做到这一点,方法是在 XSD 文件中提供命名空间
config.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd">
<context:component-scan base-package="be.smartask.api"/>
<context:annotation-config/>
<mvc:cors>
<mvc:mapping path="/**"/>
</mvc:cors>
<mvc:annotation-driven>
<mvc:message-converters>
<bean id="marshallingHttpMessageConverter"
class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter">
<property name="marshaller" ref="jaxb2Marshaller"/>
<property name="unmarshaller" ref="jaxb2Marshaller"/>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
<bean id="jaxb2Marshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
<property name="schemas">
<list>
<value>classpath:/schema/xsd/v1/smartAsk.xsd</value>
<value>classpath:/schema/xsd/v2/smartAsk.xsd</value>
</list>
</property>
<property name="packagesToScan">
<list>
<value>be.smartask.api.model.smartask.v1</value>
<value>be.smartask.api.model.smartask.v2</value>
</list>
</property>
</bean>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
首先您提供模式位置,其次是将生成 xsd 对象的包位置,最后用您新创建的 Jaxb2Marshaller
覆盖 marshallingHttpMessageConverter如果您计划在 API 中进行一些版本控制,最好提供一个命名空间,以便编组器知道哪个 xsd 文件用于哪个包
<xs:schema xmlns="smartask:v1"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
xmlns:annox="http://annox.dev.java.net"
attributeFormDefault="unqualified"
elementFormDefault="qualified"
targetNamespace="smartask:v1"
jaxb:version="2.1"
jaxb:extensionBindingPrefixes="annox">
<xs:schema xmlns="smartask:v2"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
xmlns:annox="http://annox.dev.java.net"
attributeFormDefault="unqualified"
elementFormDefault="qualified"
targetNamespace="smartask:v2"
jaxb:version="2.1"
jaxb:extensionBindingPrefixes="annox">
在我们的例子中是 "smartask:v1" 和 "smartask:v2"