gpt4 book ai didi

java - 无法使用 POST 请求和 Jackson 发送 JSON

转载 作者:行者123 更新时间:2023-12-02 03:32:35 32 4
gpt4 key购买 nike

这是我的实体类:

@Entity
@Table(name = "menuitem")
public class MenuItem {

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY )
@Column(name = "id")
private Integer id;

@Column(name = "title")
private String title;

@Column(name = "eng_title")
private String engTitle;

@Column(name = "price")
private double price;

@Column(name = "description")
private String description;

@Column(name = "consist_of")
private String consistOf;

@Column(name = "volume_value")
private double volumeValue;

@Column(name = "volume_title")
private String volumeTitle;

@ManyToOne
@JoinColumn(name = "category_id",insertable = false, updatable = false)
private Category category;

@Column(name = "category_id")
private int categoryId;

public MenuItem() {

}

public MenuItem(JSONObject jsonObject) {
if (!jsonObject.isNull("id")) {
this.id = jsonObject.getInt("id");
}

if (!jsonObject.isNull("title")) {
this.title = jsonObject.getString("title");
}

if (!jsonObject.isNull("engTitle")) {
this.engTitle = jsonObject.getString("engTitle");
}

if (!jsonObject.isNull("price")) {
this.price = jsonObject.getDouble("price");
}

if (!jsonObject.isNull("description")) {
this.description = jsonObject.getString("description");
}

if (!jsonObject.isNull("consistOf")) {
this.consistOf = jsonObject.getString("consistOf");
}

if (!jsonObject.isNull("volumeValue")) {
this.volumeValue = jsonObject.getDouble("volumeValue");
}

if (!jsonObject.isNull("volumeTitle")) {
this.volumeTitle = jsonObject.getString("volumeTitle");
}
}

public MenuItem(Integer id, String title, String engTitle, double price,
String description, String consistOf, double volumeValue,
String volumeTitle) {
super();
this.id = id;
this.title = title;
this.engTitle = engTitle;
this.price = price;
this.description = description;
this.consistOf = consistOf;
this.volumeValue = volumeValue;
this.volumeTitle = volumeTitle;
}



@Override
public String toString() {
return "MenuItem [id=" + id + ", title=" + title + ", engTitle="
+ engTitle + ", price=" + price + ", description="
+ description + ", consistOf=" + consistOf + ", volumeValue="
+ volumeValue + ", volumeTitle=" + volumeTitle + ", categoryId=" + categoryId + "]";
}

public String getEngTitle() {
return engTitle;
}

public void setEngTitle(String engTitle) {
this.engTitle = engTitle;
}

public void setId(Integer id) {
this.id = id;
}

public Integer getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

public double getPrice() {
return price;
}

public void setPrice(double price) {
this.price = price;
}

public String getDescription() {
return description;
}

public void setDescription(String description) {
this.description = description;
}

public String getConsistOf() {
return consistOf;
}

public void setConsistOf(String consistOf) {
this.consistOf = consistOf;
}

public double getVolumeValue() {
return volumeValue;
}

public void setVolumeValue(double volumeValue) {
this.volumeValue = volumeValue;
}

public String getVolumeTitle() {
return volumeTitle;
}

public void setVolumeTitle(String volumeTitle) {
this.volumeTitle = volumeTitle;
}

@JsonBackReference
@JsonIgnore
public Category getCategory() {
return category;
}

public void setCategory(Category category) {
this.category = category;
}

public void setCategoryId(int categoryId) {
this.categoryId = categoryId;
}

}

这是我的根上下文:

<beans:bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<beans:property name="messageConverters">
<beans:array>
<beans:bean
class="org.springframework.http.converter.StringHttpMessageConverter">
<beans:property name="supportedMediaTypes" value="text/plain;charset=UTF-8" />
</beans:bean>
</beans:array>
</beans:property>
</beans:bean>

<!-- Resolves views selected for rendering by @Controllers to .jsp resources
in the /WEB-INF/views directory -->
<beans:bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>

<!-- Configure to plugin JSON as request and response in method handler -->
<beans:bean
class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<beans:property name="messageConverters">
<beans:list>
<beans:ref bean="jsonMessageConverter" />
</beans:list>
</beans:property>
</beans:bean>

<!-- Configure bean to convert JSON to POJO and vice versa -->
<beans:bean id="jsonMessageConverter"
class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
</beans:bean>

<mvc:interceptors>
<beans:bean class="ru.tenet.cafe.interceptor.LoginInterceptor" />
</mvc:interceptors>

<context:component-scan base-package="ru.tenet.cafe" />

<mvc:annotation-driven />
<tx:annotation-driven transaction-manager="transactionManager" />

<beans:bean id="dataSourceMain" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<beans:property name="driverClass" value="org.postgresql.Driver" />
<beans:property name="jdbcUrl"
value="jdbc:postgresql://192.168.101.158:5432/cafe" />
<beans:property name="user" value="postgres" />
<beans:property name="password" value="123" />
<beans:property name="minPoolSize" value="5" />
<beans:property name="maxPoolSize" value="8" />
<beans:property name="preferredTestQuery" value="SELECT 1" />
<beans:property name="acquireIncrement" value="1" />
<beans:property name="idleConnectionTestPeriod" value="100" />
<beans:property name="maxStatements" value="0" />
<beans:property name="checkoutTimeout" value="60000" />
</beans:bean>

<beans:bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<beans:property name="dataSource" ref="dataSourceMain" />
<beans:property name="configLocation">
<beans:value>/WEB-INF/db/hibernate.cfg.xml</beans:value>
</beans:property>
<beans:property name="hibernateProperties">
<beans:props>
<beans:prop key="hibernate.connection.characterEncoding">UTF-8</beans:prop>
<beans:prop key="hibernate.connection.charSet">UTF-8</beans:prop>
<beans:prop key="hibernate.connection.useUnicode">true</beans:prop>
<beans:prop key="hibernate.show_sql">false</beans:prop>
</beans:props>
</beans:property>

</beans:bean>



<beans:bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<beans:property name="sessionFactory" ref="sessionFactory" />
</beans:bean>

这是我的 Controller :

@RequestMapping(value = "", method = RequestMethod.POST)
public ResponseEntity<String> create(
@RequestBody MenuItem menuItem) {
menuService.create(menuItem);
return new ResponseEntity<String>(HttpStatus.OK);

}

但是如果我发送带有以下正文的 POST 请求

{
"title":"Пепперони",
"engTitle":"Pepperoni",
"price":300,
"description":"Сами лючщи пица слющи. Тольки щто привезли дарагой.",
"consistOf":"E666, стальная стружка, вода (без ГМО)",
"volumeValue":500,
"volumeTitle":"г",
"categoryId":38
}

我会得到:

415 The server refused this request because the request entity is in a format not supported by the requested resource for the requested method.

什么鬼?

最佳答案

让我们从一个小定义开始:

415 Unsupported Media Type

The request entity has a media type whichthe server or resource does not support. For example, the clientuploads an image as image/svg+xml, but the server requires that imagesuse a different format.

可以通过以下方式解决:

@RequestMapping(value = "", method = RequestMethod.POST, produces = "application/json; charset=UTF-8", consumes = "application/json; charset=UTF-8")

说明:基本上,您需要指定端点将消耗/生成哪种数据。发送请求时不要忘记指定 header

Content-Type: application/json

关于java - 无法使用 POST 请求和 Jackson 发送 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37884760/

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