相关疑难解决方法(0)

Spring @JsonIgnore无法正常工作

如何让@JsonIgnore工作我有一堂课.即使我把注释放在那里它对输出也没有影响.我在用杰克逊.

public class QuestionBlock implements ComparableByID{


    int ID;

    String title;
    String description;
    boolean deleted;
    boolean isDraft;
    boolean visible;
    Timestamp modifiedDate;
    String modifiedBy;


    private List<Question> questions =  new ArrayList<>();

    @JsonIgnore
    private List<Survey> surveys =  new ArrayList<>();

    ...

    @JsonIgnore
    public List<Survey> getSurveys() {
        return surveys;
    }

    @JsonIgnore
    public void setSurveys(List<Survey> surveys) {
        this.surveys = surveys;
    }

}
Run Code Online (Sandbox Code Playgroud)

这是我的Controller方法:

@RequestMapping(value = "/questionBlock/{id}",produces = "application/json;charset=UTF-8")
    @ResponseBody
    public QuestionBlock getQuestionBlock(@PathVariable("id") int id) {
        return surveyService.getQuestionBlock(id);
    }
Run Code Online (Sandbox Code Playgroud)

这是我的servlet-context.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:beans="http://www.springframework.org/schema/beans" …
Run Code Online (Sandbox Code Playgroud)

java spring jackson

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

Spring MVC:@ResponseBody,415不支持的媒体类型

我无法将JSON Post映射到特定的Java对象以通过Hibernate保存它

正确设置Ajax调用的标头...

Accept          application/json
Content-Type    application/json; charset=UTF-8
Run Code Online (Sandbox Code Playgroud)

并且HTTP方法是POST

这是我的配置......

我的Spring MVC函数映射看起来像这样

@RequestMapping(value = {"/save.json"},method = RequestMethod.POST)
public ModelMap save(@RequestBody Seizure seizureObj,Model model) {
   ...
}
Run Code Online (Sandbox Code Playgroud)

在我的容器xml中,我有一个像这样的ContentNegotiatingViewResolver

<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
    <property name="mediaTypes">
        <map>
            <entry key="html" value="text/html" />
            <entry key="json" value="application/json" />
        </map>
    </property>
    <property name="viewResolvers">
        <list>
            <bean id="viewResolver"
                class="org.springframework.web.servlet.view.InternalResourceViewResolver">
                <property name="prefix" value="/WEB-INF/assets/" />
                <property name="suffix" value=".jsp" />
            </bean>
        </list>
    </property>
    <property name="defaultViews">
        <list>
            <bean
                class="org.springframework.web.servlet.view.json.MappingJacksonJsonView">
                <property name="prefixJson" value="false" />
                <property name="objectMapper" ref="jacksonObjectMapper" />
            </bean>
        </list>
    </property>
</bean> …
Run Code Online (Sandbox Code Playgroud)

hibernate spring-mvc jackson

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

标签 统计

jackson ×2

hibernate ×1

java ×1

spring ×1

spring-mvc ×1