我创建了基于Web的应用程序示例:Angular的前端和spring boot mer客户的后端)Angular URL:http://localhost:4200/spring boot URL:http://localhost:9020/(REST:http://localhost:9020/API/)
<h1>Angular Part </h1>
`export class Customer {
id: number;
firstname: number;
lastname: Number;
age: number;
active: boolean;}`
import { Customer } from './customer';
export class CustomerService {
private baseUrl = http://localhost:9020/api';
constructor(private http: HttpClient) { }
getCustomer(id: number): Observable<Object> {
return this.http.get(${this.baseUrl}+`/customers`+/${id});}
createCustomer(customer: Customer): Observable<Object> {
console.log("customer.lastname: "+customer.lastname);
console.log("customer.firstname: "+customer.firstname);
return this.http.post(${this.baseUrl} + `/create`, customer);
}
getCustomersList(): Observable<any> {
return this.http.get(${this.baseUrl}+`/customers`);
}
}
import { Component, OnInit } from '@angular/core';
import { Customer } from '../customer';
import { CustomerService } from '../customer.service';`
@Component({
selector: 'create-customer',
templateUrl: './create-customer.component.html',
styleUrls: ['./create-customer.component.css']
})
export class CreateCustomerComponent implements OnInit {
customer: Customer = new Customer();
submitted = false;
constructor(private customerService: CustomerService) { }
ngOnInit() {
}
newCustomer(): void {
this.submitted = false;
this.customer = new Customer();
}
save() {
this.customerService.createCustomer(this.customer)
.subscribe(data => {console.log(data);
this.submitted = true;},error => console.log(error));
this.customer = new Customer();}
onSubmit() {
this.save();}}
@Entity
@Table(name = "customer")
public class Customer implements Serializable {
private static final long serialVersionUID = -3009157732242241606L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
@Column(name = "last_Name")
private String lastName;
@Column(name = "first_Name")
private String firstName;
@Column(name = "age")
private int age;
@Column(name = "active")
private boolean active = true;
public Customer() {
}
public Customer(String firstName, String lastName, int age, boolean active) {
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
this.active=active;
}
public long getId() {
return id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getLastName() {
return this.lastName;
}
public void setAge(int age) {
this.age = age;
}
public int getAge() {
return this.age;
}
public boolean isActive() {
return active;
}
public void setActive(boolean active) {
this.active = active;
}
}
.
@CrossOrigin(origins = "http://localhost:4200")
@RestController
@RequestMapping("/api")
public class CustomerController {
@Autowired
CustomerRepository repository;
@PostMapping(value = "/create")
public ResponseEntity<Customer> postCustomer(@RequestBody Customer customer) {
try {
Customer _customer = repository.save(new Customer(customer.getFirstName(),customer.getLastName(),customer.getAge(),customer.isActive()));
return new ResponseEntity<>(_customer, HttpStatus.CREATED);
} catch (Exception e) {
return new ResponseEntity<>(null, HttpStatus.EXPECTATION_FAILED);
}
}
}
.
CREATE TABLE customer(
id INT NOT NULL AUTO_INCREMENT,
firstname VARCHAR(20) NOT NULL,
lastname VARCHAR(20) NOT NULL,
PRIMARY KEY (id));
应用程序.属性:
>
spring.jpa.show-sql=true`
有效载荷{“id”:518,“lastName”:null,“firstName”:null,“age”:99,“active”:true}lastName和firstName应该是字符串值,因为我在这里输入字符串输入图像描述
1:https://i.stack.imgur.com/2s8jh.png
在Entity类中,列名为“first_name”,但当您创建表时,它用“firstname”命名,下划线消失了。
基于Web中的示例创建了两个应用程序:Angular中的Frontend和customer)Angular URL:http://localhost:4200/Spring Boot URL:http://localhost:9020/(REST:http://localhost:9020/API/) 根据这里的专家对我前面问题的说明(https://stackoverflow.com/posts
问题内容: 将JSON数据从JSP传递到ResponseBody中的控制器时出错。 Ajax电话: 控制器: AppConfig.java @豆 请帮助我摆脱困境。我正在使用Spring 4,Jakson 2.3.0 如果我尝试POST请求,它将给出:org.springframework.web.HttpRequestMethodNotSupportedException:请求方法’POST’不
我有一个控制器类,其中有几个方法,其中一个是应该接受POST请求的方法。 当我试图发送一个POST请求时,我得到一个错误,说: POST请求的ContentType是应用程序/x-ww-form-urlencoded; charset=UTF-8。表单数据为: 属性包含“%”。如果删除“%”,程序将运行良好,但当正文包含“%s”时,程序将无法正常运行。
问题内容: 将JSON数据从JSP传递到ResponseBody中的控制器时出错。 Ajax电话: 控制器: AppConfig.java @豆 请帮助我摆脱困境。我正在使用Spring 4,Jakson 2.3.0 如果我尝试POST请求,它将给出:org.springframework.web.HttpRequestMethodNotSupportedException:请求方法’POST’不
我创建了一个简单的SpringBootREST服务来处理HTML页面中的发布数据。当我发布数据时,一切正常,但当我想检查@RequestMapping value href上的数据时,我看到错误消息: 白标错误页 此应用程序没有/error的显式映射,因此您将其视为回退。 周三11月15 13:33:46CET 2017有一个意外的错误(类型=坏请求,状态=400)。缺少所需的请求正文:公共org
我得到了<code>缺少的要求osgi.wiring。包…部署包时出错,缺少的包来自库(maven依赖项),因此包在其<code>类路径中 我的理解是:当使用来自另一个<code>OSGI捆绑包,但事实并非如此,这只是一个自制的库(没有部署到OSGI容器,甚至没有捆绑包),所以我一定不太明白