我在项目中使用的是Spring-boot的较新版本,因此,我使用的是getOne(Long id)(而不是findById(Long id))(可选的findById对我不起作用)。我的“保存新表单”方法可以正常工作,并且SQL数据库为提交的表单(也称为代理)创建了一个ID。但是,在编辑表单时,我无法访问现有的ID,并且收到空错误。有人可以在这里向我指出正确的方向。是因为我正在使用getOne方法使它返回null,还是导致此问题的其他原因?
这是跟踪堆栈,显示具有空ID的500错误。
2019-03-08 14:58:32.416 DEBUG 988 --- [p-nio-64-exec-1] o.s.web.servlet.DispatcherServlet : "ERROR" dispatch for POST "/error", parameters={masked}
2019-03-08 14:58:32.417 DEBUG 988 --- [p-nio-64-exec-1] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped to public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2019-03-08 14:58:32.418 DEBUG 988 --- [p-nio-64-exec-1] o.s.w.s.m.m.a.HttpEntityMethodProcessor : Using 'application/json', given [*/*] and supported [application/json, application/*+json, application/json, application/*+json]
2019-03-08 14:58:32.418 DEBUG 988 --- [p-nio-64-exec-1] o.s.w.s.m.m.a.HttpEntityMethodProcessor : Writing [{timestamp=Fri Mar 08 14:58:32 PST 2019, status=500, error=Internal Server Error, message=The given id must not be null!; nested exception is java.lang.IllegalArgumentException: The given id must not be null!, path=/saveBroker}]
2019-03-08 14:58:32.419 DEBUG 988 --- [p-nio-64-exec-1] o.s.web.servlet.DispatcherServlet : Exiting from "ERROR" dispatch, status 500
这是我的控制器功能:
@RequestMapping(value="/newBroker")
public String newBroker(Model model){
model.addAttribute("broker",new Broker());
return "brokerProfile";
}
@RequestMapping(value="/addBroker")
public String addBroker(Model model, @ModelAttribute(value="broker") Broker broker)
{
Long id=null;
try{
Broker newBroker = brokerRepository.save(broker);
id = newBroker.getId();
if (broker.getStatus().equals("active"))
broker.setOnboardedDate(LocalDate.now());
brokerRepository.save(newBroker);
}catch (DataAccessException e){
e.printStackTrace();
}
return "redirect:/edit/"+id;
}
@RequestMapping(value="/edit/{id}")
public String editbroker(Model model, @PathVariable("id") Long id, Broker broker){
Broker existing= brokerRepository.getOne(id);
model.addAttribute("broker",existing);
return "brokerProfile";
}
@RequestMapping(value="/saveBroker")
@ResponseBody
public JSONObject saveBroker(Model model, @ModelAttribute(value="broker") Broker broker)
{
Boolean saved=false;
JSONObject response=new JSONObject();
Broker brokerBeforeUpdate = brokerRepository.getOne(broker.getId());
if (brokerBeforeUpdate!=null && !brokerBeforeUpdate.getStatus().equals("active") && broker.getStatus().equals("active"))
broker.setOnboardedDate(LocalDate.now());
else if (!broker.getStatus().equals("active"))
broker.setOnboardedDate(null);
try{
brokerBeforeUpdate=brokerRepository.save(broker);
saved=true;
response.put("brokerId",broker.getId());
}catch (DataAccessException e) {
e.printStackTrace();
response.put("error",e.getLocalizedMessage());
response.put("cause",e.getLocalizedMessage());
}
response.put("success",saved);
return response;
}
}
我的存储库保留为getOne()/ findById
public interface BrokerRepository extends CrudRepository<Broker,Long>, JpaSpecificationExecutor {
Broker save(Broker entity);
// <Optional>Broker findById(Long id);
Broker getOne(Long id);
void delete(Broker entity);
List<Broker> findAll();
}
经纪人。与ID有关的Java片段
@Entity
@Table(name="Broker")
public class Broker {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "brokerId")
private Long id;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
}
答案 0 :(得分:0)
我认为您需要指定@RequestMethod中使用的方法,因为它没有默认值
@RequestMapping(path="/",method=RequestMethod.GET or POST)
但是您使用它代替@RequestMethod
@PostMethod(path='/')
@GetMethod(path='/')