Java @RequestMapping未获取参数和值。Spring framework
我正在尝试创建一个带有CRUD操作的api。为此,创建了一个用户bean和一个扩展Crudepository的用户存储库 我试图创建一个用户,但它接受空值。在数据库中创建了一个用户id,其值为null 代码: 这里出了什么问题?我是网络开发新手。请帮忙。 谢谢 编辑: 与我尝试为特定用户添加位置的方式相同。 创建了与用户实体的location bean和ManyTone关系 正在插入位置,但位置id和用户id返回nullJava @RequestMapping未获取参数和值。Spring framework,java,mysql,spring,spring-data-jpa,crud,Java,Mysql,Spring,Spring Data Jpa,Crud,我正在尝试创建一个带有CRUD操作的api。为此,创建了一个用户bean和一个扩展Crudepository的用户存储库 我试图创建一个用户,但它接受空值。在数据库中创建了一个用户id,其值为null 代码: 这里出了什么问题?我是网络开发新手。请帮忙。 谢谢 编辑: 与我尝试为特定用户添加位置的方式相同。 创建了与用户实体的location bean和ManyTone关系 正在插入位置,但位置id和用户id返回null @Entity public class Location {
@Entity
public class Location {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Integer id;
private String location;
private double latitude;
public Location() {}
@ManyToOne(fetch = FetchType.LAZY)
private User user;
@Override
public String toString() {
return "Location [id=" + id + ", location=" + location + ", latitude=" + latitude + ", longitude=" + longitude
+ "]";
}
public double getLatitude() {
return latitude;
}
public void setLatitude(double latitude) {
this.latitude = latitude;
}
public double getLongitude() {
return longitude;
}
public void setLongitude(double longitude) {
this.longitude = longitude;
}
private double longitude;
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public char[] getId() {
// TODO Auto-generated method stub
return null;
}
}
@Controller // This means that this class is a Controller
@RequestMapping(path="/Locations") // This means URL's start with /demo (after Application path)
public class LocationController {
@Autowired // This means to get the bean called userRepository
// Which is auto-generated by Spring, we will use it to handle the data
private LocationRepository locationRepository;
private UserRepository userRepository;
@RequestMapping("/create")
@ResponseBody
public Location create(@RequestBody Location location) {
String locId = "";
Location newLocation = new Location();
try {
User user = userRepository(location.getUser()); //Get the parent Object
newLocation = new Location(); //Create a new Many object
newLocation.setLatitude(location.getLatitude());
newLocation.setLongitude(location.getLongitude());
newLocation.setLocation(location.getLocation());
newLocation.setUser(user);
locationRepository.save(newLocation);
locId = String.valueOf(newLocation.getId());
}
catch (Exception ex) {
// return "Error creating the user: " + ex.toString();
return newLocation;
}
return locationRepository.save(newLocation);
}
private User userRepository(User user) {
// TODO Auto-generated method stub
return null;
}
@GetMapping(path="/all")
public @ResponseBody Iterable<Location> getAllLocations() {
// This returns a JSON or XML with the users
return locationRepository.findAll();
}
}
public interface LocationRepository extends CrudRepository<Location, Long>{
}
回应
{
"id": null,
"location": "miraroad",
"latitude": 15645,
"user": null,
"longitude": 154645
}
这里怎么了?
请帮忙。谢谢。试试这个。这应该行得通
@RequestMapping("/create")
@ResponseBody
public String create(@RequestBody User user) {
String userId = "";
try {
userRepository.save(user);
userId = String.valueOf(user.getId());
} catch (Exception ex) {
return "Error creating the user: " + ex.toString();
}
return "User succesfully created with id = " + userId;
}
并在用户类中创建一个无参数构造函数 试试这个。这应该行得通
@RequestMapping("/create")
@ResponseBody
public String create(@RequestBody User user) {
String userId = "";
try {
userRepository.save(user);
userId = String.valueOf(user.getId());
} catch (Exception ex) {
return "Error creating the user: " + ex.toString();
}
return "User succesfully created with id = " + userId;
}
并在用户类中创建一个无参数构造函数 我想将创建的用户作为json对象返回,我怎么能呢@pvpkiranInstead字符串返回类型,只返回用户对象。Spring将自动将其转换为json。只需执行
returnuserrepository.save(user)
并将方法的签名更改为returnuserThank。。如何命名返回的json对象或json数组?将@ManyToOne(fetch=FetchType.LAZY)
更改为@ManyToOne(fetch=FetchType.EAGER)
没有帮助。尝试更改我想将创建的用户作为json对象返回,我如何才能@pvpkiranInstead字符串返回类型,只返回用户对象。Spring将自动将其转换为json。只需执行returnuserrepository.save(user)
并将方法的签名更改为returnuserThank。。如何命名返回的json对象或json数组?将@ManyToOne(fetch=FetchType.LAZY)
更改为@ManyToOne(fetch=FetchType.EAGER)
没有帮助。试图改变
{
"id": null,
"location": "miraroad",
"latitude": 15645,
"user": null,
"longitude": 154645
}
@RequestMapping("/create")
@ResponseBody
public String create(@RequestBody User user) {
String userId = "";
try {
userRepository.save(user);
userId = String.valueOf(user.getId());
} catch (Exception ex) {
return "Error creating the user: " + ex.toString();
}
return "User succesfully created with id = " + userId;
}