我对AJAX + Rest + Spring boot + MySQL有问题。请求成功,但是响应为空,无论请求中使用的格式如何。实际上,请求有效负载显示[“ customer”:“ MyCustomer”],所以我认为问题出在控制器上。
AJAX 函数ajax_ciudad(customer){// onchange选中框
$.ajax({
type: "POST",
contentType: "application/json",
url: "/api/ciudades",
data: JSON.stringify(customer),
dataType: 'json',
cache: false,
timeout: 600000,
}).then(function(data) {
DO SOMETHING....
});
});
}
控制器
@RestController
@RequestMapping
public class SearchController {
@Autowired
private MyRepository myRepository;
@PostMapping("/api/ciudades")
public @ResponseBody List<?> getSedes(String customer){
List<Canal> ciudades = myRepository.findBySede(customer);
return ciudades;
}
REPOSITORY
@Query(value ="SELECT * FROM canal WHERE cliente = ?1", nativeQuery = true)
List<Canal> findBySede(@Param("customer") String customer);
...总是返回一个空对象[]
我尝试了几种数据格式,我尝试使用.val()获取Select值以获取字符串,使用.value获取html值。...如果我手动输入了myRepository.findBySede( “ MyString”)ajax响应具有信息,但是当我传递值时它不起作用;我做错了什么?谢谢您的帮助
我的实体类
@Entity
@Table(name = "canal")
public class Canal {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name = "idcanal", unique = true, nullable = false)
int id;
@Column(name = "cliente")
String cliente;
@Column(name = "ciudad")
String ciudad;
@Column(name = "sede")
String sede;
@Column(name = "canal")
String canal;
@Column(name = "ip_pe")
String ip_pe;
@Column(name = "nombre_pe")
String nombre_pe;
@Column(name = "ipwan_pe")
String ipwan_pe;
@Column(name = "puerto_pe")
String puerto_pe ;
@Column(name = "ipwan_router")
String ipwan_router;
@Column(name = "enrutamiento")
String enrutamiento;
*****getters and setters****
}
答案 0 :(得分:1)
我希望您已经正确定义了您的仓库,并根据客户字符串返回了正确的数据。
从ajax "customer"
传递为@Param
,并像这样定义您的控制器,例如@RestController
包括@Controller
和@ResponseBody:
@RestController
public class SearchController {
@Autowired
private MyRepository myRepository;
@PostMapping("/api/ciudades")
public List<Canal> getSedes(@Param("customer") String customer){
List<Canal> ciudades = myRepository.findBySede(customer);
return ciudades;
}
或者更好的方法,返回ResponseEntity。
@RestController
public class SearchController {
@Autowired
private MyRepository myRepository;
@PostMapping("/api/ciudades")
public ResponseEntity<?> getSedes(@Param("customer") String customer){
List<Canal> ciudades = myRepository.findBySede(customer);
return new ResponseEntity<>(ciudades , HttpStatus.OK);
}
在仓库中这样定义您的方法:
@Repository|
public interface MyRepository extends CrudRepository<Canal, String>
@Query(value ="SELECT * FROM canal GROUP BY cliente", nativeQuery = true)
List<Canal> findByIdcanal(); // works fines
//I assume here you have canal table and cliente is column in it as you
running native query
@Query(value ="SELECT * FROM canal WHERE cliente = ?1", nativeQuery = true)
List<Canal> findBySede(String customer); // doesn't work
//OR try this as JPQL if you have Canal is entity and cliente as string type in it.
@Query(value ="Select c from Canal c WHERE c.cliente = :cliente")
List<Canal> findBySede(@Param("cliente") String customer);
//OR simply have Named query I assume you have Canal and cliente string type
List<Canal> findByCliente(String customer);
}
更新,因为我检查了您的本机查询是否正常。
如果要在控制器中将客户(字符串)作为@RequestBody发送,请遵循以下步骤:
创建分类客户
public class Customer {
private String customer;
public String getCustomer() {
return customer;
}
public void setCustomer(String customer) {
this.customer = customer;
}
}
将请求处理为@RequestBody
@PostMapping("/api/ciudades")
public ResponseEntity<?> getSedes(@RequestBody Customer customer){
List<Canal> ciudades = myRepository.findBySede(customer.getCustomer());
return new ResponseEntity<>(ciudades , HttpStatus.OK);
}