我有一个Spring Boot应用程序,它在嵌入式Tomcat容器中作为可执行JAR运行。该应用程序公开了各种传统的RESTful服务。除了传统的RESTful服务之外,我还需要创建一个与传统servlet更相似的终结点。
我将传统的RESTful服务定义为使用标准HTTP方法(GET,POST,PUT等)并接受JSON或XML请求主体(使用适用方法时)并返回JSON或XML的终结点。我定义 traditional ,而不是像servlet那样的终结点,它们接受multipart / form-date并在调用方法后重定向客户端。
此端点签名如下
referer
标头的值的基本网址发送到该端点的表单数据是从我不拥有或控制的表单中发送的(我已经在下面包含了html表单)。因为表单中有许多奇怪的键/值对,所以我想将提交内容封装在FormSubmission
对象中。
下面是用于将数据发送到我的端点的表格
<form novalidate="" accept-charset="UTF-8" action="https://baseurl.com/brochure" enctype="multipart/form-data" id="hsForm_4b5eec88-e83b-474f-9c51-6204df71e5a5" method="POST" class="hs-form stacked hs-form-private hs-form-4b5eec88-e83b-474f-9c51-6204df71e5a5_bc44b3dd-63e5-4a6f-85d2-4c2c4b436bf8" data-form-id="4b5eec88-e83b-474f-9c51-6204df71e5a5" data-portal-id="482700" target="target_iframe_4b5eec88-e83b-474f-9c51-6204df71e5a5" data-reactid=".hbspt-forms-0">
<input id="firstname-4b5eec88-e83b-474f-9c51-6204df71e5a5" type="text" name="firstname"></div>
<input id="lastname-4b5eec88-e83b-474f-9c51-6204df71e5a5" type="text" name="lastname"></div>
<input id="address-4b5eec88-e83b-474f-9c51-6204df71e5a5" type="text" name="address"></div>
<select id="state-4b5eec88-e83b-474f-9c51-6204df71e5a5" name="state" >
<option value="" disabled="" selected="" >- Please Select -</option>
<option value="AL" >Alabama</option>
<option value="AK">Alaska</option>
</select>
<select id="country_custom-4b5eec88-e83b-474f-9c51-6204df71e5a5" name="country_custom" ">
<option value="United States">United States</option>
<option value="Canada">Canada</option>
</select>
<input id="zip-4b5eec88-e83b-474f-9c51-6204df71e5a5" class="hs-input" type="text" name="zip"></div>
<input id="email-4b5eec88-e83b-474f-9c51-6204df71e5a5" class="hs-input" type="email" name="email"></div>
<select id="want_to_talk_with_an_area_builder_-4b5eec88-e83b-474f-9c51-6204df71e5a5" name="want_to_talk_with_an_area_builder_">
<option value="Yes">Yes, now please.</option>
<option value="No">No thank you</option>
</select>
<select id="blog_pool_life_2_subscription-4b5eec88-e83b-474f-9c51-6204df71e5a5" name="blog_pool_life_2_subscription"
<option value="instant">Instant</option>
<option value="daily">Daily</option>
</select>
</form>
上面的表单将multipart / form-data发送到以下端点
@PostMapping("/brochure")
public RedirectView post( @ModelAttribute FormSubmission submission, RedirectAttributes attributes, @RequestHeader( value = "referer", required = true ) final String referer ) {
String base = getBaseURL( referer );
System.out.println( submission.getLastName() );
return new RedirectView( base );
}
FormSubmission类如下
public class FormSubmission {
private String firstName;
private String lastName;
private String street;
private String city;
private String state;
private String zip;
private String email;
private String country;
private Boolean mail;
private String speakWithBuilder;
@ModelAttribute("city")
public String getCity() {
return city;
}
@ModelAttribute("city")
public void setCity(String city) {
this.city = city;
}
@ModelAttribute("state")
public String getState() {
return state;
}
@ModelAttribute("state")
public void setState(String state) {
this.state = state;
}
@ModelAttribute("zip")
public String getZip() {
return zip;
}
@ModelAttribute("zip")
public void setZip(String zip) {
this.zip = zip;
}
@ModelAttribute("country_custom")
public String getEmail() {
return email;
}
@ModelAttribute("country_custom")
public void setEmail(String email) {
this.email = email;
}
@ModelAttribute("country_custom")
public String getCountry() {
return country;
}
@ModelAttribute("country_custom")
public void setCountry(String country) {
this.country = country;
}
@ModelAttribute("blog_pool_life_2_subscription")
public Boolean isMail() {
return mail;
}
@ModelAttribute("blog_pool_life_2_subscription")
public void setMail( String mail ) {
this.mail = mail.toLowerCase().contains( "yes" );
}
@ModelAttribute("firstname")
public String getFirstName() {
return firstName;
}
@ModelAttribute("firstname")
public void setFirstName(String firstName) {
this.firstName = firstName;
}
@ModelAttribute("lastname")
public String getLastName() {
return lastName;
}
@ModelAttribute("lastname")
public void setLastName(String lastName) {
this.lastName = lastName;
}
@ModelAttribute("address")
public String getStreet() {
return street;
}
@ModelAttribute("address")
public void setStreet(String street) {
this.street = street;
}
@ModelAttribute("want_to_talk_with_an_area_builder_")
public String getSpeakWithBuilder() {
return speakWithBuilder;
}
@ModelAttribute("want_to_talk_with_an_area_builder_")
public void setSpeakWithBuilder(String speakWithBuilder) {
this.speakWithBuilder = speakWithBuilder;
}
}
您可以看到我的端点定义了一个@ModelAttribute FormSubmission submission
,并且我的提交对象使用HTML表单中的元素名称来标记每个getter和setter。但是,打印语句显示为空。我发誓这在几天前已经奏效,现在所有值都为null。请指教。
为进一步说明,当我如下修改端点签名时
@PostMapping("/brochure")
public RedirectView post( @ModelAttribute("firstname") String firstName, RedirectAttributes attributes, @RequestHeader( value = "referer", required = true ) final String referer ) {
System.out.println( firstName );
String base = getBaseURL( referer );
return new RedirectView( base );
}
并将端点命名为成功打印。我希望这能更好地说明我的问题是关于将这些表单数据键/值对封装在一个对象中。
如果我能够成功封装数据:
@ModelAttribute
,然后@ModelAttribute
这是更好的方法,但是我仍然希望能够使用与Java命名约定一致的变量来命名,而由于我无法控制表单,因此没有ModelAttribute就无法做到这一点