gpt4 book ai didi

java - Spring MVC + JAXB : ServletException: Unable to locate object to be marshalled in model

转载 作者:行者123 更新时间:2023-12-01 15:45:42 33 4
gpt4 key购买 nike

我有一个 Web 服务,它返回枚举类型的对象列表。该枚举已使用 @XmlRootElement 以及另一个包装类型进行注释,其中包含这些枚举类型对象的列表以及另一个字符串成员。

@XmlRootElement(name = "indicator")
public enum IndicatorEnum
{
FROST_DAYS,
ICING_DAYS,
MAX_TEMPS_MUCH_ABOVE_AVG,
UNKNOWN;

public static IndicatorEnum fromInt(final int value)
{
switch (value)
{
case 1:
return FROST_DAYS;
case 2:
return ICING_DAYS;
case 3:
return MAX_TEMPS_MUCH_ABOVE_AVG;
default:
return UNKNOWN;
}
}

public static IndicatorEnum fromString(final String dataType)
{
if ("FROST_DAYS".equals(dataType))
{
return FROST_DAYS;
}
else if ("ICING_DAYS".equals(dataType))
{
return ICING_DAYS;
}
else if ("MAX_TEMPS_MUCH_ABOVE_AVG".equals(dataType))
{
return MAX_TEMPS_MUCH_ABOVE_AVG;
}
return UNKNOWN;
}

public String getValueUnits()
{
switch (this)
{
case FROST_DAYS:
return "days";
case ICING_DAYS:
return "days";
case MAX_TEMPS_MUCH_ABOVE_AVG:
return "percentages";
default:
return "UNKNOWN VALUE UNITS";
}
}

public String toDisplayString()
{
switch (this)
{
case FROST_DAYS:
return "Frost Days";
case ICING_DAYS:
return "Icing Days";
case MAX_TEMPS_MUCH_ABOVE_AVG:
return "Max Temps Much Above Average";
default:
return "UNKNOWN";
}
}

@Override
public String toString()
{
switch (this)
{
case FROST_DAYS:
return "FROST_DAYS";
case ICING_DAYS:
return "ICING_DAYS";
case MAX_TEMPS_MUCH_ABOVE_AVG:
return "MAX_TEMPS_MUCH_ABOVE_AVG";
default:
return "UNKNOWN";
}
}
}
<小时/>
@XmlRootElement(name = "available_indicators_for_station")
public class AvailableIndicatorsForStationBean
{

private List<IndicatorEnum> availableIndicators;
private String stationCode;

/**
* Default, no-arg constructor.
*/
public AvailableIndicatorsForStationBean()
{
}

/**
* Constructor.
*
* @param stationCode
* @param availableIndicators
*/
public AvailableIndicatorsForStationBean(final String stationCode,
final List<IndicatorEnum> availableIndicators)
{
this.stationCode = stationCode;
this.availableIndicators = availableIndicators;
}

@XmlElement(name = "available_indicators")
public List<IndicatorEnum> getAvailableIndicators()
{
return availableIndicators;
}

@XmlElement(name = "station_code")
public String getStationCode()
{
return stationCode;
}

public void setAvailableIndicators(final List<IndicatorEnum> availableIndicators)
{
this.availableIndicators = availableIndicators;
}

public void setStationCode(final String stationCode)
{
this.stationCode = stationCode;
}
}

我有一个返回模型和 View 的 Controller 类,如下所示:

@RequestMapping(method = RequestMethod.GET, value = "/available_indicators_for_station_xml")
public ModelAndView getAvailableIndicatorsXml(@RequestParam("station_code") final String stationCode)
{
// validate the parameters
if ((stationCode == null) || stationCode.isEmpty())
{
throw new RuntimeException("Missing required request parameter: \'station_code\'");
}

// find the matching list of Observations entities
List<IndicatorEnum> availableIndicators = stationDao.findAvailableIndicatorsForStation(stationCode);

// convert the list of indicators to a JAXB bindable model object
AvailableIndicatorsForStationBean availableIndicatorsForStationBean = new AvailableIndicatorsForStationBean(stationCode,
availableIndicators);

// pass it on as a model and view
return new ModelAndView(jaxb2MarshallingView, "available_indicators_for_station", availableIndicatorsForStationBean);
}

当我向 Web 服务发出请求时,我收到 JAXB 编码错误:

javax.servlet.ServletException: Unable to locate object to be marshalled in model: {org.springframework.validation.BindingResult.available_indicators_for_station=org.springframework.validation.BeanPropertyBindingResult: 0 errors, available_indicators_for_station=com.abc.rest.model.AvailableIndicatorsForStationBean@9eb530}
org.springframework.web.servlet.view.xml.MarshallingView.renderMergedOutputModel(MarshallingView.java:100)
org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:250)
org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1063)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:801)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:549)
javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

任何人都可以告诉我我做错了什么吗?预先感谢您的任何建议。

最佳答案

我遇到了这个异常,javax.servlet.ServletException:无法定位要在模型中编码的对象,因为我放置在模型中的对象具有公共(public)成员,我不希望将其序列化,但我没有将其注释为@ XmlTransient

关于java - Spring MVC + JAXB : ServletException: Unable to locate object to be marshalled in model,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7068628/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com