相关疑难解决方法(0)

Spring MVC中的UTF-8编码问题

我有一个Spring MVC bean,我想通过设置UTF-8编码来恢复土耳其语.但是虽然我的字符串是"şŞğĞİıçÇöÖüÜ"但它返回"??????çÇöÖüÜ".而且当我查看响应页面,即Internet Explorer页面时,编码是西欧iso,而不是UTF-8.

这是代码:

    @RequestMapping(method=RequestMethod.GET,value="/GetMyList")
public @ResponseBody String getMyList(HttpServletRequest request, HttpServletResponse response) throws CryptoException{
    String contentType= "text/html;charset=UTF-8";
    response.setContentType(contentType);
    try {
        request.setCharacterEncoding("utf-8");
    } catch (UnsupportedEncodingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    response.setCharacterEncoding("utf-8");     
    String str="??????çÇöÖüÜ";
    return str;
}   
Run Code Online (Sandbox Code Playgroud)

java spring-mvc utf-8 character-encoding

48
推荐指数
5
解决办法
12万
查看次数

如何摆脱<mvc:annotation-driven />?

到目前为止,<mvc:annotation-driven />给我带来了很多麻烦,所以我想摆脱它.尽管spring框架文档清楚地说明它应该做什么,但实际上summar的标签列表<mvc:annotation-driven /> 却缺乏.

所以我坚持删除<mvc:annotation-driven />,现在得到错误

WARN osweb.servlet.PageNotFound - 在名为'workoutsensor'的DispatcherServlet中找不到具有URI [/ webapp/trainees]的HTTP请求的映射

对于所有应该由控制器类解决的Url(在这种情况下:) ./trainees.有什么建议,我可以阅读更多关于<mvc:annotation-driven />?我非常想知道究竟是什么标签代表<mvc:annotation-driven />.

java spring servlets spring-mvc

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

使用MockServletContext进行单元测试

我已经使用Gradle 设置了spring boot应用程序.现在我明白@EnableAutoConnfiguration根据类路径中的依赖关系配置应用程序.我很高兴避免所有的管道,但事情开始发生,我希望不会.

这是我的依赖项:

dependencies {
        compile('org.springframework.boot:spring-boot-starter-web:1.2.3.RELEASE')
        compile 'org.springframework.hateoas:spring-hateoas:0.17.0.RELEASE'
        compile 'org.springframework.plugin:spring-plugin-core:1.2.0.RELEASE'
        compile 'org.springframework.boot:spring-boot-starter-data-jpa'

        compile 'com.google.guava:guava:18.0'
        compile 'com.fasterxml.jackson.datatype:jackson-datatype-jsr310'
        compile 'commons-beanutils:commons-beanutils:1.9.2'
        runtime 'org.hsqldb:hsqldb:2.3.2'

        testCompile 'org.springframework.boot:spring-boot-starter-test'
        testCompile 'com.jayway.jsonpath:json-path:2.0.0'
    }
Run Code Online (Sandbox Code Playgroud)

我的应用类:

@ComponentScan("org.home.project")
@SpringBootApplication
//@EnableHypermediaSupport(type = EnableHypermediaSupport.HypermediaType.HAL)
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}
Run Code Online (Sandbox Code Playgroud)

UserController的一个片段:

@RequestMapping(method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
public HttpEntity<ResourceSupport> create(@Valid @RequestBody UserCreateRequest ucr, BindingResult bindingResult) {
    if (bindingResult.hasErrors()) throw new InvalidRequestException("Bad Request", bindingResult);

    Long userId = userService.create(ucr);

    ResourceSupport resource = …
Run Code Online (Sandbox Code Playgroud)

java spring spring-hateoas spring-boot

3
推荐指数
1
解决办法
5288
查看次数

RestController:HTTP 状态 406 - 不可接受

我使用“Jsonitter”作为 JSON 序列化框架,并且在我的项目中没有使用 Maven。我一直在 Restful api 中通过直接将“Jsonitter”的结果写入到HttpServletResponse我发现该@RestController属性的方式来返回 JSON 对象。由于具有 ASP.Net MVC 背景,我希望框架能够根据标头自动序列化我的 api 中返回的对象Accept。但我觉得,Spring 需要第三方序列化框架来呈现结果(即 Jackson),因为它返回HTTP Status 406 - Not Acceptable结果。

我的使用方法如下:

@RestController
@EnableWebMvc
public class TestApi {
    @RequestMapping(value = "Test", method = RequestMethod.Get, produces = "application/json")
    public List<String> letsTest(){
        return myStringList;
    }
}
Run Code Online (Sandbox Code Playgroud)

我没有任何关于杰克逊的参考,我宁愿根本不使用它,我觉得错误就是由此造成的。如果没有杰克逊,有什么办法可以完成这项工作吗?

java spring-mvc

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