【问题标题】:XML incorrect response Spring REST and hibernateXML 不正确的响应 Spring REST 和休眠
【发布时间】:2018-07-03 05:55:43
【问题描述】:

我的项目中有 Spring REST 和休眠。我想以 xml 格式显示响应,但无论我在 URL 中传递的 id 是什么,我都会得到不正确的 xml 响应,如下所示:

<COUNTRY id="0">
<population>0</population>
</COUNTRY>

我点击的网址是:

http://localhost:8080/SpringRestHibernateExample/getCountry/2

在调试时,我发现 id 被正确传递到 DAO 层,并且正确的国家也被获取。不知何故,渲染没有正确发生。 这是我的课

控制器

@RequestMapping(value = "/getCountry/{id}", method = RequestMethod.GET, 
headers = "Accept=application/xml",
        produces="application/xml")
public Country getCountryById(@PathVariable int id) {

    return countryService.getCountry(id);
}

型号

@XmlRootElement (name = "COUNTRY")
@XmlAccessorType(XmlAccessType.FIELD)
@Entity
@Table(name="COUNTRY")
public class Country{

@XmlAttribute
@Id
@Column(name="id")
@GeneratedValue(strategy=GenerationType.IDENTITY)
int id;

@XmlElement
@Column(name="countryName")
String countryName; 

@XmlElement
@Column(name="population")
long population;

public Country() {
    super();
}

服务

@Transactional
 public Country getCountry(int id) {
    System.out.println("service"+id);
    return countryDao.getCountry(id);
}

public Country getCountry(int id) {
    System.out.println("dao"+id);
    Session session = this.sessionFactory.getCurrentSession();
    Country country = (Country) session.load(Country.class, new Integer(id));

    return country;
}

有人可以帮忙吗...

编辑:用 get 替换 load 解决了这个问题。但现在 /getAllCountries 我收到以下错误:

The resource identified by this request is only capable of generating 
responses with characteristics not acceptable according to the request 
"accept" headers.

下面是控制器

 @RequestMapping(value = "/getAllCountries", method = 
 RequestMethod.GET,produces="application/xml",
        headers = "Accept=application/xml")
 public List<Country> getCountries() throws CustomerNotFoundException{

 List<Country> listOfCountries = countryService.getAllCountries();
 return listOfCountries;
}

【问题讨论】:

  • 不要使用load。使用get。也不要使用new Integer,直接使用id(创建new Integer会增加开销)。
  • @M.Deinum 它与 get() 一起使用.. 你能告诉我为什么它没有与 load() 一起使用吗?
  • load 创建实际对象的代理而不实际访问数据库,get 将实际从数据库中检索对象。如果正在进行的事务仍然处于活动状态,load 将起作用,但是由于您不在服务方法之外,因此情况并非如此,并且代理无法从数据库中获取实际数据。
  • @M.Deinum 是的,即使我对使用 load() 有疑问。我现在对 /getAllCountries 有不同的错误。你能帮我吗?我已经更新了问题
  • 这是一个完全不同的问题,不要在一个问题中问 2 个问题。

标签: java xml spring hibernate rest


【解决方案1】:

问题是你的 DAO 方法它使用Session.load 而不是Session.get

loadget 之间的区别在于load(通常总是)返回一个惰性代理。它只会在实际请求数据时获取实际的底层数据(由于延迟检查数据库,这也可能导致很晚EntityNotFoundExceptions)。现在通常你不会注意到任何懒惰的东西(也许在性能方面),但在这种情况下你会注意到。您不在活动事务中(因此是Session),因此代理无法再从数据库中获取所需的数据(并且因为没有任何东西你会得到0,因为这是默认的int)。

【讨论】:

    【解决方案2】:

    我建议将参数名称添加到@PathVariable

    示例@PathVariable("id")

    另外,如果你使用一个对象作为 ID,你可以像一个整数一样在所有层中使用它而不是 int。

    此外,如果不需要,Integer 可以为 null,在 PathVariable 中放入属性 required = true。如果不允许为 null

    最后,如果您不打算对对象进行操作并直接返回它,请使用 session.get 而不是 session.load

    【讨论】:

      猜你喜欢
      • 2023-02-17
      • 1970-01-01
      • 2011-09-21
      • 1970-01-01
      • 2016-11-13
      • 2016-10-27
      • 1970-01-01
      • 1970-01-01
      • 2017-08-06
      相关资源
      最近更新 更多