相关疑难解决方法(0)

Jackson 2.0与Spring 3.1

Spring MVC 3.1与Jackson 2.0兼容吗?Spring MVC是否会在类路径上自动检测Jackson,并且使用JSON内容类型的请求委托给Jackson仍有效?

java spring json spring-mvc jackson

28
推荐指数
4
解决办法
5万
查看次数

Spring Rest应用程序中的“不支持内容类型'application / json; charset = UTF-8'”

当我在localhost:8080 / api / users上执行POST请求以创建新用户时,出现以下错误:

{
"timestamp": "2018-05-28T09:44:55.704+0000",
"status": 415,
"error": "Unsupported Media Type",
"message": "Content type 'application/json;charset=UTF-8' not supported",
"path": "/api/users/"
}
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

是请求的正文,已选择JSON(application / json)。即使我删除了角色并将其保留为空,它也会给出相同的错误。

在此处输入图片说明

标头的内容类型也是application / json。

在此处输入图片说明

这是我的控制器:

@PostMapping("/api/users" )
public User createUser(@Valid @RequestBody User user) {
    securityService.autologin(user.getUsername(), user.getPassword());
    return userService.createUser(user);
}
Run Code Online (Sandbox Code Playgroud)

UserService中的createUser函数:

   public User createUser(@Valid @RequestBody User user) {
    user.setPassword(bCryptPasswordEncoder.encode(user.getPassword()));
    user.setRoles(new HashSet<>(roleRepository.findAll()));
    return userRepository.save(user);
}
Run Code Online (Sandbox Code Playgroud)

编辑

这是我的User类:

@Entity
@Table(name = "user")
@EntityListeners(AuditingEntityListener.class)
@JsonIgnoreProperties(value = {"createdAt", "updatedAt"}, 
    allowGetters = true)
public class User implements Serializable{

private …
Run Code Online (Sandbox Code Playgroud)

java rest spring httprequest postman

6
推荐指数
2
解决办法
2万
查看次数

spring boot mvc - 不支持内容类型“application/json;charset=UTF-8”

这个 Spring Boot 项目中,我在POST使用(使用 Postman)新Item资源时出现错误

Resolving exception from handler 
     [public com.example.demo.resource.Item com.example.demo.controller.ItemController.addItem(com.example.demo.resource.Item)]: 
     org.springframework.web.HttpMediaTypeNotSupportedException: 
     Content type 'application/json;charset=UTF-8' not supported
Run Code Online (Sandbox Code Playgroud)

在请求正文中,我复制Item了从GET请求中获得的现有s之一(并更改了iditemName

    // Request body:
    {
        "id": 10, // also tried without id field as it's autogenerated
        "itemName": "milk",
        "cart": {
            "id": 1
        }
    }
Run Code Online (Sandbox Code Playgroud)

我确保我在Item课堂上有正确的 getter 和 setter (因为这是一个已知问题

@Entity
@Table(name="items")
@Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
@JsonIdentityInfo(
          generator = ObjectIdGenerators.PropertyGenerator.class, 
          property = "id")
public class Item
{
    @Id …
Run Code Online (Sandbox Code Playgroud)

java spring spring-mvc spring-boot

4
推荐指数
1
解决办法
9450
查看次数

标签 统计

java ×3

spring ×3

spring-mvc ×2

httprequest ×1

jackson ×1

json ×1

postman ×1

rest ×1

spring-boot ×1