gpt4 book ai didi

java - 为什么我取不回 JSON 内容?

转载 作者:行者123 更新时间:2023-12-01 11:35:56 24 4
gpt4 key购买 nike

我正在尝试使用 Spring REST API 从数据库中获取对象。问题是我没有正确接收内容。

我的日志输出:

/hello
Hibernate: select this_.id as id0_0_, this_.countryCode as countryC2_0_0_ from Country this_ where this_.countryCode=?
Found: at

我使用的 Controller :

import java.util.Iterator;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.mahlzeit.datamodel.HibernateTest;
import com.mahlzeit.datamodel.address.Country;

@RestController
public class HelloWorldController {

@RequestMapping(value = "/hello")//, method = RequestMethod.GET)
public Country hello(ModelMap model) {

System.out.println("/hello");

HibernateTest hbt = new HibernateTest();

Session session = hbt.sessionFactory.openSession();
Criteria cr = session.createCriteria(Country.class);
cr.add(Restrictions.eq("countryCode", "at"));

List<Country> queryAustria = cr.list();

if (queryAustria.isEmpty() == true) {
System.err.println("at not found");
return null;
}

Country austria = null;
for (Iterator<Country> iterator = queryAustria.iterator(); iterator
.hasNext();) {
Country country = (Country) iterator.next();
if (country.getCountryCode().equals("at")) {
austria = country;
break;
}
}

System.out.println("Found: "+ austria.getCountryCode());

return austria;
}
}

所以,我正在从 Hibernate 取回我想要的对象,但我实际上取回的是

HTTP Status 404

这是Country.java:

@Entity
public class Country implements Serializable {
private static final long serialVersionUID = -2060021861139912774L;

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;

@NotNull
@Column(length=2,unique=true)
private String countryCode;

// Setter & Getter ..
}

配置文件:

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

<context:component-scan base-package="com.mahlzeit.server.mobile" />

<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>

</beans>

最佳答案

如果您使用 InternalResourceViewResolver那么 Spring 将尝试解析 View基于view name你正在经过。所以它会寻找文件 /WEB-INF/austria.jsp假设 Controller 返回的值为“奥地利”。这就是您得到 404 的原因错误。

因此,要解决此问题,请添加 <mvc:annotation-driven />在 Spring 配置文件中。

<?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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"

xsi:schemaLocation="http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">

<context:component-scan base-package="com.mahlzeit.server.mobile" />

<mvc:annotation-driven />

<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>

</beans>

此外,您还需要在类路径中包含与 JSON 相关的 Jars -- WEB-INF/lib文件夹:

<强> jackson-core-asl-1.9.13.jar & jackson-mapper-asl-1.9.13.jar

关于java - 为什么我取不回 JSON 内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29994154/

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