Spring rest没有返回我的对象​​json

这是我的控制器:

@Controller
@RequestMapping("api")
public class RestController {
    @RequestMapping(value="check",method=RequestMethod.GET,produces="application/json")
    @ResponseBody
    public UserAcc returnuser(){
        return UserDao.getUserById(1);
    }
}

...这是我的UserAcc类:

@XmlRootElement
@Entity
@Table(name = "user_acc")
@NamedQueries({
@NamedQuery(name = "UserAcc.findAll", query = "SELECT u FROM UserAcc u")})
public class UserAcc implements Serializable {
private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "user_id")
    private Integer userId;

    @Basic(optional = false)
    @Lob
    @Column(name = "user_name")
    private String userName;

    @Basic(optional = false)
    @Lob
    @Column(name = "Company")
    private String company;

    @Basic(optional = false)
    @Column(name = "user_email")
    private String userEmail;

    @Basic(optional = false)
    @Column(name = "is_account_activated")
    private boolean isAccountActivated;

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "assigenedToId")
    private List<Issues> issuesList;

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "assosiateId")
    private List<Projectdetails> projectdetailsList;

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "moduleassignedtoid")
    private List<DevelAcc> develAccList;

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "assignedUserId")
    private List<TestAcc> testAccList;

    // ... code ommitted

}

但我没有从userAcc对象获得任何json响应。 有什么问题吗? 我的弹簧配置工作正常。 任何帮助表示赞赏!


您需要使用库(如杰克逊)将UserAcc类转换为json。 举例来说,请参考http://www.journaldev.com/2552/spring-restful-web-service-example-with-json-jackson-and-client-program。

@XmlRootElement
public class UserAcc {
    .... fields
}

假设你的类路径中有所有必需的依赖关系(如杰克的答案中已经提到的那样),你可以像这样尝试:

@Controller
@RequestMapping("/api")
public class RestController {
   @RequestMapping(value="/check",method=RequestMethod.GET,produces="application/json")
   public @ResponseBody UserAcc returnuser(){
     return UserDao.getUserById(1);;
   }
}

...或者像那样:

@Controller
@RequestMapping("/api")
public class RestController {
   @RequestMapping(value="/check",method=RequestMethod.GET,produces="application/json")
   public @ResponseBody Map<String, Object> returnuser(){
     UserAcc acc = UserDao.getUserById(1);
     Map<String,Object> map = new HashMap<>();
     map.put("id",""+acc.getId());
     //... and so on
     return map;
   }
}

我最近使用Jackson实现了这个功能(用于将POJO响应转换为JSON)。 首先,我将Jackson依赖关系添加到我的项目中(我正在使用Maven):

    <!-- Jackson for Java - JSON conversion -->
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
        <version>${jackson-2-version}</version>
    </dependency>

    <!-- Jackson for Java - JSON conversion -->
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>${jackson-2-version}</version>
    </dependency>

    <!-- Jackson for Java - JSON conversion -->
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-annotations</artifactId>
        <version>${jackson-2-version}</version>
    </dependency>

然后,Spring将自动使用Jackson为任何返回“应用程序/ json”响应的Controller方法(如您所遇到的情况)生成JSON响应。

如果您想用一些字符来预先加载JSON响应以防止JSON劫持,那么将其包含在您的Spring配置文件中:

<mvc:annotation-driven>
    <!-- Set JSON converter (for converting Java to JSON in REST response --> 
    <mvc:message-converters>
        <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
            <!-- Add prefix to JSON response to prevent JSON hijacking -->
            <property name="prefixJson" value="true" />
        </bean>
    </mvc:message-converters>
</mvc:annotation-driven>
  • 这将在这个字符串前加上所有的JSON响应:{} &&(在处理响应之前应该由调用客户端剥离)。
  • 链接地址: http://www.djcxy.com/p/8385.html

    上一篇: Spring rest not returning my object json

    下一篇: Compatibility of the "Origin" http header for enforcing restrictions