简体   繁体   English

XML错误响应Spring REST和休眠状态

[英]XML incorrect response Spring REST and hibernate

I have Spring REST and hibernate in my project. 我有Spring REST,并且在我的项目中处于休眠状态。 I want to display the response in xml format but irrespective of the id I pass in the URL I am getting the incorrect xml response as below: 我想以xml格式显示响应,但是无论我在URL中传递的id如何,我都会得到如下所示的不正确的xml响应:

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

The URL that I hit is : 我点击的网址是:

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

Upon debug I have found that the id is correctly getting passed till the DAO layer and also the correct country is getting fetched. 通过调试,我发现ID正确传递到DAO层,并且正确的国家/地区也已获取。 Somehow the rendering is not happening correctly. 渲染不正确。 Here are my classes 这是我的课

Controller 控制者

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

    return countryService.getCountry(id);
}

Model 模型

@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();
}

Service 服务

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

DAO

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;
}

Can someone please help... 有人可以帮忙吗...

EDIT : replacing load with get solved the issue. 编辑:用获取替换负载解决了该问题。 But now for /getAllCountries I am receiving the below error: 但是现在对于/ getAllCountries我收到以下错误:

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

Below is the controller 下面是控制器

 @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;
}

The problem is your DAO method it uses Session.load instead of Session.get . 问题是您的DAO方法使用Session.load而不是Session.get

The difference between load and get is that load (generally always) returns a lazy proxy. loadget之间的区别是load (通常总是)返回一个惰性代理。 It will only obtain the actual underlying data when data is actually requested (which could also lead to very late EntityNotFoundException s due to delayed checking in the database). 它仅在实际请求数据时才获取实际的基础数据(由于数据库中的延迟检查,这也可能导致EntityNotFoundException异常延迟)。 Now generally you wouldn't notice any of the lazy stuff (maybe in performance) but in this case you do. 现在,通常您不会注意到任何懒惰的东西(也许在性能上),但是在这种情况下,您会注意到。 you are outside of an active transaction (and thus Session ) and due to that the proxy cannot obtain the needed data from the database anymore (and because there isn't anything you will get 0 as that is the default for an int ). 您处于活动事务之外(因此处于Session ),并且由于该代理不再能够从数据库中获取所需的数据(并且由于没有任何东西,您将获得0因为这是int的默认值)。

I recommend to add to the @PathVariable the name of the parameter 我建议将参数名称添加到@PathVariable

Example @PathVariable("id") 示例@PathVariable(“ id”)

Also if you are using an object as ID, you can like an integer use it in all the layer instead the int. 同样,如果您使用对象作为ID,则可以像整数一样在所有层中使用它,而不是int。

Also, the Integer could be null if is not desired, put in the PathVariable the attribute required = true. 另外,如果不需要,则Integer可以为null,将Pathdariable设置为required = true。 if null is not allowed 如果不允许为null

Finally if you are no planning to operate with the object and return it directly use session.get instead of session.load 最后,如果您不打算使用该对象并直接将其返回,请使用session.get而不是session.load

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM